3 ms·
If you need to optimize this, on modern CPUs, you'd probably want to always run all tests and have just one exit, instead of multiple possible paths. That's eas
by vardump 7y ago
If you need to optimize this, on modern CPUs, you'd probably want to always run all tests and have just one exit, instead of multiple possible paths. That's easier said than done, because "&&" typically does generate a branch.
In other words, doing significantly more computational work but less conditional branching can run significantly faster, because modern CPUs are built for this kind of code.
Once you achieve completely branchless collision test, you've completely eliminated branch misprediction penalties inside it.
I've sped up existing code too many times simply by removing if-statement checking if the work needs to be done... :-)
That said, I find it highly unlikely 2D collision testing optimization running on modern CPU yields any measurable performance improvement.
- peterwoerner 7y agoA nice trick I have used for similar problems (although finite element related) is (x + abs(x))/2 returns x if x>0 and 0 if x<0.