Search code examples
javaraytracing

Java raytracer increase CPU usage


I tried to implement a raytracer in Java and everything works fine. Once, I was rendering a scene and I checked the CPU-Usage of my program. Every virtual core was being used but the overall CPU-Usage was around 22-23%

I know that when I was rendering something with Cinema4D every virtual core was being used 100%.

How could I archive this? Will it make my raytracer run 4 times faster? (23% -> 100%)


Solution

  • You should definitely be able to get to 100% CPU usage, and all else being equal, doing so should result in faster rendering speed. That you're apparently not at 100% indicates to me that you're either not creating enough threads, or your threads are blocking on something.

    I've found the sweet spot is one rendering thread per virtual core. You can use the API to detect the number of cores available at runtime, and spawn that many rendering threads. For an example of this, see my java ray tracer.

    Every virtual core was being used but the overall CPU-Usage was around 22-23%

    I'm not sure what you mean by this, but you may want to dig into it using a performance profiler to better understand how your threads are using CPU. Ideally you want each of your threads to be working all the time. I use VisualVM to visualize process threads.