运算速度

发布于 2024-10-15 07:51:21 字数 122 浏览 3 评论 0原文

我正在编写一个游戏,在渲染代码中进行速度计算非常重要。

如何获取某些操作的速度?

例如,如何知道乘法是否比 sqrt 等更快?或者我必须进行测试并计算时间。

编程语言是c++,谢谢。

I'm coding a game and it's very important to make speed calculations in render-code.

How can I get the speed of some operations?

For example, how to know whether multiplying is faster then sqrt, etc? Or I have to make tests and calculate the time.

Programming language is c++, thanks.

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

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

发布评论

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

评论(4

非要怀念 2024-10-22 07:51:21

这种微观优化只会浪费你的时间以获得最小的收益。

使用分析器,并从分析器告诉您游戏花费大部分时间的地方开始改进您自己的算法和代码。

请注意,在某些情况下,您可能必须彻底修改整个软件 - 或其中的主要部分 - 才能实现更有效的设计。在这种情况下,分析器结果可能会误导没有经验的人。例如,与一劳永逸地缓存其结果相比,优化复杂的计算可能会获得最小的增益。

另请参阅这个有点相关的线程

This kind of micro-optimisation is just the thing to waste your time for minimal gain.

Use a profiler and start by improving your own algorithms and code wherever the profiler tells you that the game is spending most of its time.

Note that in some cases you may have to overhaul the whole software - or a major part of it - in order to implement a more efficient design. In that case the profiler results can be misleading to the inexperienced. E.g. optimising a complex computation may procure minimal gain, when compared to caching its result once and for all.

See also this somewhat related thread.

习惯成性 2024-10-22 07:51:21

确定特定操作的速度通常称为分析。分析操作的最佳解决方案是使用分析器。 Visual Studio 有一个很好的分析器。 Linux 有 gprof 。如果您的编译器没有分析器,并且您经常对代码进行分析,那么购买具有分析器的编译器可能是值得的。

如果您必须在不使用专业分析器的情况下完成任务,那么您通常可以通过将自己的分析器嵌入到您的程序中

查看以获取某些分析器的代码。

Determining the speed of a particular operation is often known as profiling.The best solution for profiling an operation is to use a profiler. Visual Studio has a good profiler. Linux has gprof . If your compiler doesn't have a profiler, it might be worthwhile purchasing a compiler that does if you will often be profiling your code.

If you have to get by without using a professional profiler, then you can usually get by embedding your own into your program

check this out for codes of some profilers.

你是我的挚爱i 2024-10-22 07:51:21

最好的选择是使用 AQTime 等工具并进行分析运行。然后你就会知道该把时间花在哪里优化。但过早地或基于猜测工作可能不会给你带来太多好处,只会使你的代码复杂化或破坏某些东西。最好的办法是,如果可以的话,将任何浮点计算(尤其是 sin、cos 等)和 sqrt 从任何循环中取出。

我曾经有过这样的事情:

for i = 0 to nc
  for j = 0 to nc
      aij = sqrt(a[i]*b[j])

它计算 nc*nc 平方根。但由于 sqrt(a*b) 等于 sqrt(a)*sqrt(b),因此您可以预先计算所有 a 和 b 的平方根,这样循环就变成如下所示。因此,您得到的是 2*nc 平方根,而不是 nc*nc 平方根。

for i = 0 to nc
  for j = 0 to nc
      aij = asqrt[i]*bsqrt[j]

Your best bet is to use a tool like AQTime and do a profiling run. Then you will know where to spend your time optimizing. But doing it prematurely or based on guess work likely wont get you much, and just complicate your code or break something. The best thing is to take any floating point calculations, especially sin, cos and the like, and sqrt out of any loops if you can.

I once had something like this:

for i = 0 to nc
  for j = 0 to nc
      aij = sqrt(a[i]*b[j])

which calculates nc*nc square roots. But since sqrt(a*b) is equal to sqrt(a)*sqrt(b), you can precompute the square roots for all the a's and b's beforehand so the loop then just becomes what is shown below. So instead of nc*nc square roots, you have 2*nc square roots.

for i = 0 to nc
  for j = 0 to nc
      aij = asqrt[i]*bsqrt[j]
硬不硬你别怂 2024-10-22 07:51:21

您提出的问题很大程度上取决于您在硬件级别开发的平台。不仅不同的芯片组(Intel / AMD)之间会存在差异,而且平台上也会存在差异(我怀疑 iPhone 没有那么多指令来更快地完成某些事情)。

您在问题中声明您正在谈论“渲染代码”。如果您谈论的是实际在 GPU(着色器代码)而不是 CPU 上运行的代码,则规则会发生巨大变化。

正如@thkala 所说,在你开始之前我真的不会担心这个。我发现它不仅更容易,而且更快地以首先有效的方式编码,然后(仅当需要改进时)重写在分析代码时速度较慢的部分。与尝试仅使用特定函数相比,更好的算法通常会提供更好的性能。

在我们为 iPhone 开发的游戏中,我唯一记住的是大型数学运算 (sqrt) 很慢(不是基本数学),而每帧运行的 for 循环可以很快吃掉CPU。记住这一点,我们几乎不需要优化任何代码 - 因为无论如何它都以 60fps 运行 - 所以我很高兴我一开始就没有担心它。

The question you are asking is highly dependent on the platform you are developing for at the hardware level. Not only will there be variation between different chipsets (Intel / AMD) but there will also be variations on the platform (I suspect that the iPhone doesn't have as many instructions for doing certain things quicker).

You state in your question that you are talking about 'render code'. The rules change massively if you're talking about code that will actually run on the GPU (shader code) instead of the CPU.

As @thkala states, I really wouldn't worry about this before you start. I've found it not only easier, but quicker to code it in a way that works first, and then (only if it needs improving) rewriting the bits that are slow when you profile your code. Better algorithms will usually provide better performance than trying to make use of only specific functions.

In the game(s) that we're developing for the iPhone, the only thing that I've kept in mind are that big math operations (sqrt) are slow (not basic maths) and that for loops that run every frame can quickly eat up CPU. Keeping that in mind, we've not had to optimise hardly any code - as it's all running at 60fps anyway - so I'm glad I didn't worry about it at the start.

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