Profiling in lua

There is already a profiling method in the DLL - cvStopWatch if memory serves correct - but it's useless for Lua as you're running in a multi-threaded system (where most of the threads are handling graphic sprites) so you're at the mercy of what else is happening in game at the time.

There are several references/resources that have profiled typical variations of Lua constructs and made comparisions/recommendation (eg don't use ipairs/iterators if you know the min/max range of an array) - much better to read, understand and follow those for most modders than to try and figure out where 0.03 of a sec is going.
 
Hmmm. I was thinking more in terms of trying to compare two general approaches to the same problem. For example (not saying it's the best example); loop through a players cities in doturn checking for threats, or try and use SetUnitXY in some fancy way. Of course, now that I have your fix to return the plot city, that particular question is no longer as pressing on my mind. I will dig up some of those resources of profiling lua, too. Thanks :)
 
A real profiler would be nice. You can do only so much with os.clock(). You have to add it to the code to test, and sometimes os.clock() is slower than the code you want to test.

If you just want to compare two or more Lua scripts (run many times with replication), look in the spoiler in the OP here.
 
Here is a more concrete example of what I am trying to profile:

There appear to be two main ways I can find to detect threat to a city. One is to check the plot owner and city every time a unit moves in UnitSetXY which will get called dozens of times per turn. The vast majority of calls will be negatives (no threat). So it's what I call programming for the exception. Overhead (slight if done right but still overhead) added to every turn.

The second way involves walking the plots of a city to check for nearby threatsin PlayerDoTurn. It's also programming for the exception (sigh) and it's not perfect but I plan to extend the DLL to add an iterator (along with a bunch of other missing iterators) for city plots, that would make it more effective.

Both add a lot more overhead than I'd like, but such is life. Neither is perfect either, and I end up having to code for edge cases (like city radius is smaller than city's attack range).

I'm in general a bit frustrated with how hard it is to detect threat to a city.
 
os.clock is the one I'd found before, that just returns the nearest second. That's an eternity for code.

1/1000s of a second:
Code:
> os.clock()
717.968
> os.clock()
1206.582
> os.clock()
1209.651
> os.clock()
1240.148
Still an eternity, yes. But you can time a function with 1000 repeats.
 
Back
Top Bottom