Forum

> > CS2D > Scripts > How to determine the speed of code?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to determine the speed of code?

7 replies
To the start Previous 1 Next To the start

old How to determine the speed of code?

Bowlinghead
User Off Offline

Quote
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?

old Re: How to determine the speed of code?

DC
Admin Off Offline

Quote
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.

old Re: How to determine the speed of code?

VADemon
User Off Offline

Quote
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

old Re: How to determine the speed of code?

Yates
Reviewer Off Offline

Quote
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

old Re: How to determine the speed of code?

DC
Admin Off Offline

Quote
@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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview