English How to determine the speed of code?

7 replies
Goto Page
To the start Previous 1 Next To the start
Up
Bowlinghead
User
Offline Off
Hi there,
I sometimes see people that compare code efficency by execution time like here.
How can you create them (also for other languages)?
What do these values say?
Do they apply on every computer?
Share time limited free games here
01.12.16 12:35:14 pm
Up
Hajt
User
Offline Off
Code:
1
2
3
4
5
local x = os.clock()

-- your code to benchmark

print(os.clock() - x)


https://www.lua.org/pil/22.1.html
01.12.16 03:13:09 pm
Up
DC
Admin
Offline Off
What @user Hajt: says is how to do it. Let me explain what it means:

The returned value is the time in seconds the code snippet took to run / to be executed (you basically compare the system time from before and after running the script). In CS2D this means that CS2D itself will stop for that amount of time whenever you run this code (because it's not multi threaded). Only servers run Lua scripts so if you're a server it will even pause updating rendering and everything and you will easily notice it as lag/fps stuttering.
As client you will also notice a lag. The graphics will still update but you might get a ping spike and movements/actions of other players might lag for a moment because the server is busy running the Lua script and it can't update the other stuff in that time.

So especially if a Lua snippet is executed frequently, the time it takes to run it should be super low.
1.XXX for example would mean one second which is way too much for most hooks
0.5XXX would be half a second which is still too much for frequent execution
0.1XXX would be one 1th of a second which is still a lot. This might be okay if it is executed sometimes but you shouldn't do it every frame because the server would lag a lot.

Scripts attached to hooks which are executed very frequently, like every frame or when a player moves or shoots, execution time should be a few millseconds maximum (1 millseconds (short ms) is 1/1000th of a second, so 0.001)

And no, the values are not the same on every PC because every PC is using different hardware and is running different apps / has different CPU loads. So even on the same PC the values can differ a bit if you measure the same stuff multiple times. These values only give you a rough idea on how fast/slow your script is.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
01.12.16 03:20:46 pm
Up
VADemon
User
Offline Off
For his values I hope that wasn't the time for one single run.

The following procedure:
• for-loop, because most functions are so fast that you can't measure 1 run
• the longer a loop the more accurate the comparison between functions
• the result is always different on other hardware, even your current load will affect the results (don't run benchmarks whilst playing Crysis 3 ) - process priority and stuff
• with os.clock() you will get the time your Lua process is running and the difference between start and end times is how long your function took to execute. Afterwards you can calculate the difference in percent
01.12.16 03:23:12 pm
Up
Yates
Reviewer
Offline Off
user VADemon has written:
• the result is always different on other hardware, even your current load will affect the results (don't run benchmarks whilst playing Crysis 3 ) - process priority and stuff

I am going to test this with my rig when I get home
01.12.16 08:21:58 pm
Up
Bowlinghead
User
Offline Off
Thanks for all of your replys.
If I understood it correctly, its not a really good method to determine the speed of code since this number changes on every run.
Share time limited free games here
02.12.16 07:34:17 am
Up
ohaz
User
Offline Off
Run a profiler: List of Lua Profilers, Example
https://ohaz.engineer - Software Engineering
02.12.16 08:14:33 pm
Up
DC
Admin
Offline Off
@user Bowlinghead: No, as I already said: The values normally only differ a tiny bit when running on the same machine. Also executing stuff many times in a loop (like user VADemon said) can help to get better values. You could for instance run a snippet 100,000 times and then divide the resulting time by 100,000 so you get the average time for a single run.

Therefore this method is still a very useful one. It helps a lot to get an idea of the performance of your script AND: If you try to improve a script you can very easily use this approach so see if your changes changed performance to the better or to the worse.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 Next To the start