Java 与 C++ - 光线追踪

发布于 2024-12-04 20:58:13 字数 119 浏览 1 评论 0原文

我用 Java 创建了简单的光线追踪器作为一个业余爱好项目,但是它很慢。虽然速度不是特别慢,但还是很慢。我想知道使用 C 或 C++ 等较低级语言是否可以获得任何性能提升,或者差异可以忽略不计,我应该坚持改进“我的”算法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夜司空 2024-12-11 20:58:13

我认为这个问题的答案是肯定的,在 99.99% 的情况下,非解释性语言将比虚拟机下的相同算法运行得更快。
这就是说(在java和c/c++中的图像处理方面做了很多工作,其中内存和时间很重要)我认为你应该首先尝试优化你的代码,这是我的建议:

  • 尝试使用分析器。很多我们有时会忽略的事情都可以用这样的工具来解决(比如类型转换、不必要的对象创建、应该首先优化的最关键的功能)探查器必须是你的朋友。

然后(我能看到的光线追踪的几个例子):

  • 用查找表替换 tan/sin/cos(只要可以)或 近似函数
  • 尝试处理每个数组而不是每个样本的数据
  • 尝试使用多个线程
  • ......

现在这些东西“很好”但如果速度对你来说真的很重要,我不建议使用 ac 或 c++ 语言(即使你可以),但更有可能关注 OpenCL。这可能是最好的工具,最适合构建光线追踪引擎。想象一下,您不是在谈论 30% 的改进,而是更有可能 10'000%(快 100 倍) 这是一个 java 接口: http://jogamp.org/jocl/www/
祝你好运 :-)

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 :-)

哽咽笑 2024-12-11 20:58:13

这将取决于。使用 C/C++ 将允许您完成在 Java 中无法完成的事情。 (例如 SIMD)

换句话说,我会说是的,通常可以在 C/C++ 中做得更好,但这需要一些工作。首先进行所有基本(数学/算法)优化。然后再进行微优化。

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.

巴黎盛开的樱花 2024-12-11 20:58:13

AMD 刚刚发布了一个名为 Aparapi 的开源项目,该项目在运行时将 Java 字节码转换为 OpenCL。如果您的代码无法转换为 OpenCL(存在限制)或者您没有可用的 OpenCL,则代码将在线程池中运行。

可能非常适合您的需求。

http://aparapi.googlecode.com

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

审判长 2024-12-11 20:58:13

由于您只知道实施背后的细节,因此很难回答。如果您的方法主要是数学方法,那么 Java 在幕后进行了各种优化,我认为您不会通过切换到 C++ 看到太多改进。

如果您使用大量外部库,并且根据您将光线追踪结果显示到屏幕的方法,可能会改进转向基于 C 的实现。

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.

独享拥抱 2024-12-11 20:58:13

如果您使用低效的算法,并且还要经历大量学习新语言的麻烦,那么切换到 C/C++ 会给您带来边际收益。正确编写的 Java 可以达到类似 C/C++ 代码大约 70-80% 的速度,对于非商业光线追踪器来说应该足够好了。我认为光线追踪器现在功能已完成,因此我的建议是学习如何使用分析器来检测代码中的瓶颈。请记住 80/20 规则(或者是 90/10、75/25 左右?),即您的程序花费 80% 的时间运行 20% 的代码。

更好的算法通常比语言切换能带来更好的性能提升。

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.

夜无邪 2024-12-11 20:58:13

光线追踪的效率取决于您的加速结构。使用 C++ 而不是 Java 肯定会有帮助。但是,如果您缺乏 BVH 或 Kd 树等高效结构,那么您使用的任何语言的光线追踪器都会很慢。

如果只是一个爱好,我建议还是留在Java上。如果您想加载复杂的模型,例如斯坦福佛或 Thai,那么您绝对应该转向 C++ 并开始阅读“基于物理的渲染”:http://www.pbrt.org/ 您可以在 http://pbrt.org/pbrt-2ed-chap4.pdf

简而言之,您可以根据项目的目标回答您的问题。简单的爱好=留在Java上。具有复杂模型的实时 RT=C++

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++

潜移默化 2024-12-11 20:58:13

几年前我用 Java 做了一个简单的光线追踪器。对于非常简单的网格(著名的茶壶 3D 网格和兔子 3D 网格),我可以通过实时渲染进行计算。所以我想你也可以这样做 =)

如果这是爱好,那就坚持使用 Java,不值得转向 C++。不要改变语言,而是思考可以在哪里改进代码(在 log(n) 时间内找到被光线击中的三角形、多线程编程等......)

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...)

若有似无的小暗淡 2024-12-11 20:58:13

我的猜测是,使用 c 或 c++ 您会看到显着的性能提升。您可以尝试使用之类的工具转换代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文