C++ tutorials

Started by
93 comments, last by taby 1 year, 7 months ago

taby said:
You're missing the point. In the event where it does matter, a 42-long if-else statement is generally slower than a function pointer. Like I said, look at Julia 4D 2. It proves my point.

True, I just wasn't referening to that. On this point we can agree, though its also very obvious. In the event of something that is not obvious, unless you account for the points I mentioned you are better off just not testing at all because you are going to come to the wrong conclusions. For example, the two numbers you posted previously for the code that I just viewed, are entirely meaningless. I'll do my own test again, but its very possible that for the function-pointer vs std::function, both will end up having the same performance when called, when done properly. And that is a significant difference to the results the your numbers suggested.

My other point would be to also never post performance-numbers without the associated code, but I'd digress.

Advertisement

It’s worth noting that for the last if-else case, the time complexity is of order O(n). For the function pointer, it is of O(1). At some point, O(1) wins out. It’s not monkey business, it just seems difficult to understand for a stunned twat.

taby said:
It’s worth noting that for the last if-else case, the time complexity is of order O(n). For the function pointer, it is of O(1). At some point, O(1) wins out. It’s not monkey business, it just seems difficult to understand for a stunned twat.

Even so, the actual point of where one or the other becomes can vary strongly, and needs to be evaluated properly.

As discussed and verified in a recent thread here, a linear search of O(n) can easily outperform a binary search of O(log n) until you get into multiple thousands of elements, due to the nature of how CPU&RAM works.

Similarily, a O(1) hashmap will get beaten by a linear-vector with a linear-search until you get into the hundreds to thousands of elements.

In your example with the if-else vs the function-pointer, the actual underlying reason to why the function pointer outperforms so easily, is that the overhead of a function-pointer call is small. On assembly level, this is just a single indirect call instruction, so the cost will be above a direct un-inlined function call, but below a virtual-function call (since the address can be loaded directly and does not need to be looked up in a table). Obviously, making 42 comparisons is slower. Though, since I don't what exactly those if-else where checking, if all you did was index==0; index==1 then a switch would have also done the job.

But none of the easy-to-infer stuff could be applied to any of the other examples, from this post or the original point that I was originally responding to.

hehehe… another war

how entertaining… : )

LOL, it's all good.

you’re welcome

taby said:

you’re welcome

For all of these things you suggest as good ideas about performance forget about them because these are all micro optimisations that only work in the context you found that fix in. I would recommend pulling everything through a profiler once you start optimising your code and looking at actual data that tells you which parts of your code are slow. You can use the integrated profiler that comes with VS to do hotpath tracing or use https://developer.amd.com/amd-uprof/​​ or vtune those will get you instruction level information, or use something like optick. That last one is how optimisation is done at a lot of triple A companies, usually with internal tools but they achieve the same thing.

When you start optimising at the level of function pointer vs if-else structure there are very few guidelines that are applicable to all situations, the only one that really stands up at that point is: “Run a profiler and see what it tells you is slow”. Also run the profiler before and after your change and compare the results because otherwise you know nothing.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Isn’t it so much better when we don’t call each other names?

I wasn’t saying you’re welcome to you guys. Someone thanked me, but their post was deleted.

I simply dropped all of the formulas except for the custom formula in Julia 4D 3. it’s kind of a moot point then, even though I gave multiple examples related to physics / math. It’s a computer, for heaven’s sake.

This topic is closed to new replies.

Advertisement