One of our comrades, Peter Mortensen , asked question on SuperUser about application which could consume arbitrarily percentage of CPU time. It piqued my interest.
My question is how could such application be made:
Here are some of my general ideas and why I think that they are bad:
- A calculation program could be made which will use algorithms which will be inefficient on purpose. The problem with that option is that application will basically have one gear. To solve that we would need several algorithms of various inefficiencies in order to provide more that one gear. Still, user wouldn't be able to select say 50% utilization. He'd have to choose one of the algorithms and see how it performs on his computer.
- Application could be made using a worker thread which will be set to sleep from time to time. This way user could control how long thread sleeps between each execution and control CPU utilization. However, while mean utilization would be controllable, at one moment the thread would either be sleeping or consuming resources, so it's not very good solution.
- This would be a variant of number one. Application might be made in such way as to make use of cache misses or speculative compiling in Java. For example a loop could be made which would control calculation and have at user set interval make a cache miss. This would slow down computation but it would be difficult to implement. Or in Java JIT optimizations could be used. A loop with a condition would execute in such way that JIT optimizes away the condition. Once the condition is hit, JIT would have to deoptimize the code and execute it. But if it stores both optimized and unoptimized versions of code, it will only work once. Also, something like that would require great knowledge of Java optimizations in order for it to work.
Any other ideas on how to make a program deliberately slow?
I was primarily thinking about implementing something like this in C, C++, C# or Java, but I fear that C# or Java may be too high level for this kind of problem.