Better Performance with large math calculations?

Hi everyone,

My game is pretty simple graphics wise (not much happening on-screen), but I need to do some pretty big calculations. I’m currently running the game at 60fps but only doing the math every 30fps to help lower CPU usage.

Example: Having a number of 950 quintillion and adding 4 quadrillion to it. I do this multiple times per frame like I mentioned, and the CPU usage is basically always maxed out doing tons of calculations like this. What’s the best way to improve this? I’m generally only doing addition, but I also do a couple divisions and multiplications each time as well (30 fps).

Thanks!

I can’t specifically answer but the #1 performance boost step for these things is to localize the function

local mRandom = math.random() mRandom(34)

That’s mainly for doing lots of calls though, not a single “big” call. Not really sure how math functions handle big numbers though unless I’m wrong up to 9 quntillion is still a 64-bit integer…

Thanks for the super quick reply. Yeah, I already localize all math functions like that. You’re right about it handling up to 9 quintillion as a 64bit int. My numbers go above that pretty easily though. Currently looking around to find general lua math optimization tricks as well. 

If adding large numbers took longer than adding small numbers (it doesn’t, right? This is lua after all…), then one potential optimization question would be - how much precision do you need? You could divide the initial numbers by 10^9 or something, do your small number calcs, and multiply back…

But it takes just as long to add small variables as large - given that lua doesn’t use integers… I’d guess you’re best improvements will come in (literally) re-factoring your math (moving some things out of the main calculations) / changing your algorithm (breaking the 30 fps calculations into one 1 fps calc doing more heavy lifting, and a lighter 30 fps calc, or some other such reorganization).

I can’t specifically answer but the #1 performance boost step for these things is to localize the function

local mRandom = math.random() mRandom(34)

That’s mainly for doing lots of calls though, not a single “big” call. Not really sure how math functions handle big numbers though unless I’m wrong up to 9 quntillion is still a 64-bit integer…

Thanks for the super quick reply. Yeah, I already localize all math functions like that. You’re right about it handling up to 9 quintillion as a 64bit int. My numbers go above that pretty easily though. Currently looking around to find general lua math optimization tricks as well. 

If adding large numbers took longer than adding small numbers (it doesn’t, right? This is lua after all…), then one potential optimization question would be - how much precision do you need? You could divide the initial numbers by 10^9 or something, do your small number calcs, and multiply back…

But it takes just as long to add small variables as large - given that lua doesn’t use integers… I’d guess you’re best improvements will come in (literally) re-factoring your math (moving some things out of the main calculations) / changing your algorithm (breaking the 30 fps calculations into one 1 fps calc doing more heavy lifting, and a lighter 30 fps calc, or some other such reorganization).