是否有可能推出一个显着更快的modf版本 [英] Is it possible to roll a significantly faster version of modf

查看:441
本文介绍了是否有可能推出一个显着更快的modf版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个我正在分析的应用程序中,我发现在某些情况下,这个函数能够占用总执行时间的10%以上。

MSVC ++ 2008编译器是正在使用,供参考...我不记得是否modf映射到一个单一的指令,如果有可能有任何方法,使其更快。



另见< a href =https://stackoverflow.com/questions/2637700/c-is-it-possible-to-roll-a-significantly-faster-version-of-sqrt>这里的类似问题sqrt函数

不像sqrt,我真的不知道modf是如何工作的。是否有装配操作?例如,你可以这样做:

pre $ mod $(float input,int& intPart,float& floatPart)
{
intPart =(int)input;
floatPart = input - intPart;
}

但是我认为这会受到铸造/转换等的惩罚。快速的实现工作?

解决方案

你在这里得到了很好的答案,但其他90%的时间去哪里? p>

不要看每个例程独占的时间百分比。

看看 >包含每行代码的时间百分比,如果可能的话,包括阻塞时间,而不仅仅是CPU时间。



这样,你可能会发现在这段时间内,你甚至不需要在 modf 函数或其他函数中。



获取这些信息的简单方法是

补充:当您发现可以做的优化时,预计总执行时间会减少,但不要期望百分比一定要下去。如果你摆脱了其他的东西,你的 modf 和/或 sqrt 中的%时间实际上可能会增加,或者如果你发现可以记住它们,因此,例如调用它们。)在这种优化方法中,您可以将程序的执行历史视为一个大调用树,以及您正在寻找的是什么是整个分支,你可以删除。更重要的是,由于一行代码可以出现在调用树的多个分支中,所以修剪它可以完全修剪它。


In an app I'm profiling, I found that in some scenarios this function is able to take over 10% of total execution time.

MSVC++ 2008 compiler is being used, for reference... I don't recall if modf maps to a single instruction or if there is likely any way to make it faster.

see also here for similar question on sqrt function

Unlike sqrt, I don't really know how modf works. Is there an assembly operation? For instance you could do:

modf(float input,int &intPart, float &floatPart)
{
 intPart= (int)input;
 floatPart= input - intPart;
}

But I assume this incurs penalties from casting/conversion, etc. how does a fast implementation work?

解决方案

You're getting good answers here, but where does the other 90% of time go?

Don't look at exclusive % time per routine.

Look at inclusive % time per line of code, and if possible, have it include blocked time, not just CPU time.

That way, you may well find out that part of the time, you don't even need to be in the modf function, or other functions.

An easy way to get that information is this technique.

Added: As you find optimizations that you can do, expect total execution time to decrease, but don't expect the percentages to necessarily go down. Your % time in modf and / or sqrt may actually go up if you get rid of other stuff, or they may go down if you find that you can memoize them (and therefore call them less), for example.

In this approach to optimization, you can think of the program's execution history as a big call tree, and what you are looking for is whole branches that you can prune off. What's more, since a line of code can appear in multiple branches of the call tree, pruning it in one prunes it in all.

这篇关于是否有可能推出一个显着更快的modf版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆