Sign in to follow this  
MARS_999

Test math function speed the correct way?

Recommended Posts

I am looking for what would be correct way to test the various math libs out there, to see which ones are faster than others...

 

e.g. dot() cross() normalize() ect....

 

 

code please...

 

Thanks!

Share this post


Link to post
Share on other sites

I am looking for what would be correct way to test the various math libs out there, to see which ones are faster than others...

 

e.g. dot() cross() normalize() ect....

 

 

code please...

 

Thanks!

Profiling is the way. Go search for a c++ profiler and then write a bunch of test cases with defined input and output. Run them multiple times with the profiler, swapping out the math lib with each test run. That will tell you which lib is faster, at least as far as your test cases are concerned.

 

I don't know what your platform is, but just search for "profiling c++ code" and you should find what you're looking for.

Share this post


Link to post
Share on other sites

Profiling is the way. Go search for a c++ profiler and then write a bunch of test cases with defined input and output. Run them multiple times with the profiler, swapping out the math lib with each test run. That will tell you which lib is faster, at least as far as your test cases are concerned.
 
I don't know what your platform is, but just search for "profiling c++ code" and you should find what you're looking for.

 

If only it were that simple.. for such small target functions a naive benchmark like "run these tests a few times for each library and pick the fastest one" is going to be completely plagued with accuracy issues, cache interference, and other difficulties that will render the measurements useless. You really need to mind the hardware in these instances. The PDF linked by DonTzzy is a good start (it shows how to negate cache effects by doing a warm-up run before each test, how to avoid the operating system getting in your way by directly polling the CPU, and so on). Many general-purpose profilers won't do that, as they target much larger code paths which can be tested much more deterministically.

Edited by Bacterius

Share this post


Link to post
Share on other sites

So I can't use something as simple as SFML's built in timer function calls? and just run say 5000 dot() or cross() calls? I guess if someone could show some puesdo code here so I can get the correct format on how I should approach this as I would like it to be somewhat legit so I can make a informed decision on the various libs I am looking at, unless someone can tell me which of these is faster

 

glm

CML

Irrlicht built in

SFML

 

I need some of the functionality of glm or CML mainly for OpenGL purposes and CML has some very nice functions for other calculations that I wish glm would add...

 

THanks!

Share this post


Link to post
Share on other sites

So I can't use something as simple as SFML's built in timer function calls? and just run say 5000 dot() or cross() calls? I guess if someone could show some puesdo code here so I can get the correct format on how I should approach this as I would like it to be somewhat legit so I can make a informed decision on the various libs I am looking at, unless someone can tell me which of these is faster

 

glm

CML

Irrlicht built in

SFML

 

I need some of the functionality of glm or CML mainly for OpenGL purposes and CML has some very nice functions for other calculations that I wish glm would add...

 

THanks!

 

While I agree that would be very convenient, the real world sadly does not work that way.

 

Many programmers treat the CPU as a magic black box.  Instructions go in, magic happens, and the next instruction gets queued.

 

If you are writing performance critical code you must get out of that mindset.  You must actually learn how processors work on the inside.  You must learn about cache effects, learn about the CPU pipeline, learn about bus transfers and hardware wait states and other details that most programmers would rather not learn.

 

The CPU is extremely sensitive to context. If you run a test in a tight loop you may get very good results because of cache use, branch prediction, reordering for pipeline optimizations, and other wonderful features. But if you run the exact same test under different conditions you may see very poor results because of cash misses, branch mis-predictions, and pipeline stalls.

 

 

The exact same code may be fast in one situation and slow in another.  It depends entirely on context.

 

If all your data is perfectly arranged for the CPU cache you might be able to zoom along getting instructions done "for free", getting many billions of instructions executed per second.  If your data is improperly arranged you might get abysmal performance, plummeting into a few hundred thousand instructions executed per second.

 

I have also wished I could pull up quick benchmarks to rate how fast a library is.  Many times I have wished for realistic benchmarks that matched the problems my code was trying to solve.  Performance is a very complex thing that depends almost exclusively on context.  Consequently it cannot easily be pinned down by a few general benchmark numbers.  

 

Good benchmarks are extremely hard to write, and even then a good benchmark will only serve as a guideline which may not actually apply in your specific situation.

 

 

Often the best you can do is write your code to make it work, then profile the code to identify the slow parts, fix the slow parts, and call it good enough.

Share this post


Link to post
Share on other sites

I agree with you frob, realistically the test isn't going to be a one size fits all... :(

 

but I was curious to how I could try it with a rough idea to see if there was a HUGE difference so far from what I can tell no...

 

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this