Java vs. C++ - Raytracing

ⅰ亾dé卋堺 提交于 2019-12-12 13:03:29

问题


I created simple ray tracer in Java as a hobby project, and well, it's slow. Not dramatically slow, but slow nevertheless. I wonder if I can get any performance gain using lower level language like C or C++ or will the difference be negligible and I should stick to improving "my" algorithm?


回答1:


I think the question have been answered as YES a not interpreted language will in 99.99% of the cases run faster than the same algorithm under a VM. This said (having worked a lot in image processing both in java and c/c++ where memory and time mattered) I think you should at first try to optimize your code, here are are my advises:

  • Try to find the bottleneck of your code using a profiler. A lot of things we sometimes omit can be reveled with such tools (like type casting, unnecessary creation of objects, most critical functions which should be optimized at first) A profiler have to be your friend.

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

  • Replace tan/sin/cos by lookup tables (as long as you can) or approximate functions
  • Try to process data per array and not per sample
  • Try using multiple threads

Now those things are "good" but if the speed is really critical for you, I would not suggest to use a c or c++ language (even if you could) but more likely to focus on OpenCL. This is probably the best tool available and most adapted for building ray tracings engine. Just imagine you are not talking there of an improvement of 30% but more likely 10'000% (100x faster) Here is a java interface: http://jogamp.org/jocl/www/ Good luck :-)




回答2:


It will depend. Using C/C++ will allow you access to things that you can't do in Java. (such as SIMD)

In other words, I'd say yes, it's usually possible do better in C/C++, but it will take some work. Do all your basic (mathematical/algorithmic) optimizations first. Then micro-optimize later.




回答3:


AMD just released an open source project called Aparapi which converts Java bytecode to OpenCL at runtime. If your code can't be converted to OpenCL (there are restrictions) or if you don't have OpenCL available the code will run in a Thread Pool.

Might be ideal for your needs.

http://aparapi.googlecode.com




回答4:


As you only know the details behind your implementation this is hard to answer. If your approach is mostly mathematical then Java has all kinds of optimizations going on behind the scenes here and I don't think you will see much in the way of improvements by switching to C++.

If you are using a lot of external libraries, and depending on your method of displaying the raytraced result to screen there may be improvements moving to a C-based implementation.




回答5:


Switching to C/C++ will give you marginal gains if you are using inefficient algorithms in addition to a whole cartload of headaches learning the new language. Properly written Java can achieve roughly 70-80% speed of similar C/C++ code and should be good enough for a non-commercial ray-tracer. I assume the ray-tracer is now functionally complete so my recommendation would be to learn how to use profilers to detect bottle-necks in your code. Remember the 80/20 rule (or is 90/10, 75/25 or so?) where your program spends 80% percent of its time running 20% of its code.

Better algorithms usually give better performance boosts than language switches.




回答6:


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

If it is just a hobby, I suggest to stay on Java. If you want to load complex models such as Stanford Buddha or Thai, then you definitely should move to C++ and start reading "Physically Based Rendering": http://www.pbrt.org/ You can download the Chapter 4 for free at http://pbrt.org/pbrt-2ed-chap4.pdf

In few words, you can answer your question based on the objectives of your project. Simple hobby=stay on Java. Real-time RT with complex models=C++




回答7:


I've done a simple raytracer in Java a few years ago. For pretty simple mesh (the famous teapot 3D mesh and the rabbit 3D mesh) I could do the computation with a real time rendering. So I guess you can do it too =)

If it's hobby, stick with Java, it's not worth it moving to C++. And instead of changing the language, think where you can improve your code (finding the triangle that is hit by the ray in log(n) time, multithreaded programming, etc...)




回答8:


My guess is you will see a significant performance gain with c or c++. You can try converting your code using a tool like this.



来源:https://stackoverflow.com/questions/7448508/java-vs-c-raytracing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!