Double Parser
Posted: Sat Mar 10, 2012 12:43 pm
Hi everyone,
I'm currently working on a small online game, mainly to become familiar with networking in lua (the server is written in java, the client in löve).
Everything works so far, aside from the game freezing for a very short time every few seconds on my (very slow) netbook. It's not really bad, but quite annoying. I have figured out, that the problem lies in my double parser (that obviously parses doubles from the recieved datagrams). The server sends out a datagram every 0.1 second and every datagram has six double values that need to be parsed. However, every few seconds, lua takes over 0.05 seconds to parse the package and exactly at that moment the game freezes very shortly.
I was wondering if anyone is able to give me some tips on how to optimize my parser or willing to share his implementation:
A datagram package contains doubles as defined in http://en.wikipedia.org/wiki/IEEE_754-2008
I'm currently working on a small online game, mainly to become familiar with networking in lua (the server is written in java, the client in löve).
Everything works so far, aside from the game freezing for a very short time every few seconds on my (very slow) netbook. It's not really bad, but quite annoying. I have figured out, that the problem lies in my double parser (that obviously parses doubles from the recieved datagrams). The server sends out a datagram every 0.1 second and every datagram has six double values that need to be parsed. However, every few seconds, lua takes over 0.05 seconds to parse the package and exactly at that moment the game freezes very shortly.
I was wondering if anyone is able to give me some tips on how to optimize my parser or willing to share his implementation:
A datagram package contains doubles as defined in http://en.wikipedia.org/wiki/IEEE_754-2008
Code: Select all
-- dataStream is the string containing the double, startIndex defines, where the double starts
function parseDouble(dataStream, startIndex)
return (-1)^(getSign(dataStream, startIndex)) * 2^(getExponent(dataStream, startIndex) - 1023) * (1 + getMantisse(dataStream, startIndex) / 2^(52))
end
function getMantisse(dataStream, startIndex)
tempDouble = (string.byte(dataStream, startIndex + 1) % 16) * 256^6
for i=2,7 do
tempDouble = tempDouble + string.byte(dataStream, startIndex + i) * 256^(7-i)
end
return tempDouble
end
function getExponent(dataStream, startIndex)
local exponent = string.byte(dataStream, startIndex + 1) - (string.byte(dataStream, startIndex + 1) % 16)
exponent = exponent / 16 + (string.byte(dataStream, startIndex) % 128) * 16
return exponent
end
function getSign(dataStream, startIndex)
if string.byte(dataStream, startIndex) > 127 then
return 1
else
return 0
end
end