Originally Posted by Vladinator
This is what I found, obviously the built in code handles it much faster.
The code I used: http://pastebin.com/Xfc2wgpF
...
Traversing used 0.6973 ms while using the table key used only 0.0016 ms. So with a lot of data it's more efficient to find using keys, rather than traversing and matching, really.
|
This is because the internal storage method for table keys uses a hash indexing, Lua doesn't need to transverse an array internally to find a value. Even if it did, the C code would run many times faster than running Lua code through its own scripting engine.
For future reference, I'd suggest using
debugprofilestart() and
debugprofilestop() to benchmark CPU time spent.
debugprofilestop() returns the time from when
debugprofilestart() was last called with much higher precision than comparing timestamps from
GetTime(). This lack in precision is what's causing the 0 ms readings and happens a lot when trying to compare Lua C functions and internal Lua processing.
The limit on
GetTime() precision is because it's merely a Lua wrapper for the C function
gettickcount() from the system kernel that returns an integer describing system uptime in milliseconds.
GetTime() simply returns this integer divided by 1000.