Java vs C ++ - Raytracing

I created a simple ray tracer in Java as a hobby project, and well, it is slow. Not very slow, but slow though. I wonder if I can get a performance boost using a lower level language such as C or C ++, or the difference will be insignificant, and should I stick to improving my algorithm?

+3
source share
8 answers

I think that the answer was YES, and not an interpreted language will work 99.99% faster than the same algorithm under a virtual machine. That said (working a lot on image processing in both java and c / C ++, where memory and time mattered). I think you should try to optimize your code first, here are my tips:

  • Try to find the bottleneck of your code using the profiler. Many things that we sometimes omit can be appeased by such tools (such as type casting, unnecessary creation of objects, the most important functions that should be optimized at the beginning). The profiler should be your friend.

Then (just a few examples that I could see for raytracing):

  • Replace tan / sin / cos with lookup tables (if possible) or approximate functions
  • Try to process the data on each array, not on the sample
  • Try using multiple threads.
  • ...

Now these things are โ€œgoodโ€, but if speed is really important to you, I would not suggest using c or C ++ (even if I could), but more likely to focus on OpenCL. This is perhaps the best tool available and best suited to create a ray engine. Imagine that you are not talking about an improvement of 30%, but more likely 10'000% (100 times faster). Here is the java interface: http://jogamp.org/jocl/www/ Good luck :-)

+2
source

It will depend. Using C / C ++ will allow you to access what you cannot do in Java. (e.g. SIMD)

In other words, I would say yes, in C / C ++ it is usually possibly better, but it will take some work. First, do all the basic (mathematical / algorithmic) optimizations. Then micro-optimize later.

+3
source

AMD has just released the open source Aparapi project, which converts Java bytecode to OpenCL at runtime. If your code cannot be converted to OpenCL (restrictions exist), or if you do not have OpenCL, the code will be run in the thread pool.

May be perfect for your needs.

http://aparapi.googlecode.com

+3
source

Since you only know information about your implementation, it is difficult to answer. If your approach is mostly mathematical, then Java has all sorts of optimizations happening here backstage, and I don't think you will see many improvements when you switch to C ++.

If you use a large number of external libraries, and depending on your method of displaying the raytraced result for display, there may be improvements moving to a C-based implementation.

+2
source

The effectiveness of ray tracing depends on your acceleration structure. Using C ++ instead of Java will certainly help. However, if you lack an efficient structure such as BVH or Kd-tree, your ray tracer will be slow in any language you use.

If this is just a hobby, I suggest staying in Java. If you want to download complex models such as Stanford Buddha or Thai, then you definitely need to switch to C ++ and start reading "Physical Based Rendering": http://www.pbrt.org/ You can download Chapter 4 for free on http : //pbrt.org/pbrt-2ed-chap4.pdf

In a few words, you can answer your question based on the goals of your project. A simple hobby = staying in Java. RT in real time with complex models = C ++

+2
source

I made a simple raytracer in Java a few years ago. For a fairly simple grid (the famous 3D grid teapot and 3D rabbit grid), I could perform calculations with real-time rendering. So I think you can do it too =)

If this is a hobby, stick with Java; you should not switch to C ++. And instead of changing the language, think about where you can improve your code (find the triangle that hits the beam in log (n), multi-threaded programming, etc.)

+1
source

Switching to C / C ++ will give you the ultimate benefit if you use inefficient algorithms in addition to a solid load of headaches learning a new language. Properly written Java can achieve approximately 70-80% of the speed of similar C / C ++ code and should be good enough for a non-commercial ray tracer. I assume that the ray tracer is now functionally complete, so I would suggest learning how to use profilers to detect bottlenecks in your code. Remember the 80/20 rule (or 90/10, 75/25 or so?), Where does your program spend 80% of its time executing 20% โ€‹โ€‹of the code.

Improved algorithms usually provide better performance improvements than language switches.

+1
source

I assume that you will see a significant increase in performance with c or C ++. You can try to convert your code with a tool like this .

0
source

Source: https://habr.com/ru/post/1385704/


All Articles