Why is my CPU usage so high when running casino game simulations?
Hi all, I've got a CPU thrashing problem in a Java script that's simulating casino games (roulette, 21 etc.). It's part of an article I'm writing for a game wiki. Can anyone explain to me why this is happening? Is it the random numbers or some other part of the program? TIA!
3 Answers
There are two probable causes of high CPU load when simulating a casino game. First, random number generation (creating pseudo-random numbers in a loop is very CPU-intense, especially in JS). Second, inefficient looping and memory management (e. g. too many iterations without pauses). You might try to implement throttling or use Web Workers. Check if there are any superfluous calculations or data copying when you perform iterations. Use Dev Tools in your browser to find the function that takes most time. Is there anything else you would like me to check?
CPU intensive stuff are simulated gambling apps. Something is spinning the wheel, dealing cards, etc. All day long. Your random number generator is running 24x7. Math.random() (JavaScript) is not the fastest algorithm. Optimize your loops. Or use a better RNG. Too many iterations of a simulation in a short time will block the thread. Insert delays or divide tasks. CPU thrashing usually means that the game loop is running too many things at once. Reduce it.
If you are simulating casino games, such as where a roulette ball will land or what cards a player should have dealt to them, you’re probably relying on a random number generator (RNG), and that’s very compute intensive. Generating a random number for each of 1000s of roulette pockets or shuffled cards and iterating over them is going to slow things down considerably, and if you’ve got multiple nested loops, that’s only going to add to the problem. Try grouping your calls to the RNG or replacing it with an array of shuffled cards.