内联电源功能更换

编程入门 行业动态 更新时间:2024-10-28 10:25:57
本文介绍了内联电源功能更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

只是想要一个意见。事实上,我有一个算法需要以尽可能快的速度运行。它已经太慢了。我已经做了尽可能多的算法改变,因为我可以减少代码量,所以现在我转向 微优化。 一个运行数百次的函数是来自 math.h的pow()函数。但是我已经意识到90%的时间,指数 将是0. 99.9%的时间,指数将介于-3和3之间。 所以我读到,如果 案例表达式几乎是连续的整数,那么开关'有时可以优化为跳转表。所以我编写了这个 的小伙伴: 静态内联双 mult_power(double x,int y) { 开关((y)) { 案例0:返回1.0; case -3:return 1.0 /((x)*(x)*(x)); case -2:return 1.0 /((x)*(x)); case -1:return 1.0 /(x); case 1:return(x); case 2:return(x)*(x ); 案例3:return(x)*(x)*(x); 默认值:return pow((x),(y)); } } 我实际上没有测试过这个vs在中间坚持0(我这是将会很快完成)但这是一个合理的替代品吗?或者是否有一个更有效的解决方案?

Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I''ve done as much algorithmic changes as I can to reduce the amount of code, so now I''m turning to micro-optimizations. One function that gets run a bazillion times is the pow() function from math.h. However I''ve realized probably 90% of the time, the exponent will be 0. 99.9% of the time, the exponent will lie between -3 and 3. So I read that switch''s can sometimes be optimized into jump tables if the case expressions are nearly contigous integers. So I coded up this little diddy: static inline double mult_power(double x, int y) { switch ((y)) { case 0 : return 1.0; case -3 : return 1.0 / ((x)*(x)*(x)); case -2 : return 1.0 / ((x)*(x)); case -1 : return 1.0 / (x); case 1 : return (x); case 2 : return (x)*(x); case 3 : return (x)*(x)*(x); default : return pow((x), (y)); } } I actually havent tested this vs sticking the 0 in the middle (which I will be doing shortly) but is this a sound replacement? Or is there perhaps a more efficient solution?

推荐答案

ch *********** @ gmail 写道: ch***********@gmail wrote: 我实际上没有测试过这个vs在中间坚持0(我很快就会做b $ b)但是这是一个合理的替代品吗?或者是否有一个更有效的解决方案?? I actually havent tested this vs sticking the 0 in the middle (which I will be doing shortly) but is this a sound replacement? Or is there perhaps a more efficient solution?

这取决于编译器的智能程度。聪明的编译器已经检查了简单的情况 并为此生成内联代码,这不是不可想象的 。唯一的方法就是尝试 吧。 如果你的编译器有些愚蠢,它可能有助于使它成为一个宏, 所以不会产生任何电话。 哦,你会得到一些人认为任何类型的b 投机运行的瑕疵时间与C.无关。

Well it depends on how smart your compiler is. It''s not unimaginable that a smart compiler would already be checking for the simple cases and generating in-line code for this. Only way to find out is to try it. If your compiler is somewhat dumb, it may help to make this a macro, so no call gets generated. and Oh, you''ll get some flamitude from folks that consider any kind of speculation of run-times has nothing to do with C.

ch *********** @ gmail 写道: 只是想要一个意见。事实上,我有一个算法需要以尽可能快的速度运行。它已经太慢了。我已经做了尽可能多的算法改变,因为我可以减少代码量,所以现在我转向微调优化了微优化。 Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I''ve done as much algorithmic changes as I can to reduce the amount of code, so now I''m turning to micro-optimizations.

我不确定这是否是这个问题的正确新闻组。不是这个组中的很多人都可以理解你究竟在说什么 这里。

I''m not sure this is the right newsgroup for such a question. Not a lot of people in this group can fathom what exactly you are saying here.

一个运行数百次的函数是来自 math.h的pow()函数。但是我已经意识到90%的时间,指数 将是0. 99.9%的时间,指数将介于-3和3之间。 所以我读到,如果 案例表达式几乎是连续的整数,那么开关'有时可以优化为跳转表。所以我编写了这个 的小伙伴: 静态内联双 mult_power(double x,int y) { 开关((y)) { 案例0:返回1.0; case -3:return 1.0 /((x)*(x)*(x)); case -2:return 1.0 /((x)*(x)); case -1:return 1.0 /(x); case 1:return(x); case 2:return(x)*(x ); 案例3:return(x)*(x)*(x); 默认值:return pow((x),(y)); } } 我实际上没有测试过这个vs在中间坚持0(我这是将会很快完成)但这是一个合理的替代品吗? One function that gets run a bazillion times is the pow() function from math.h. However I''ve realized probably 90% of the time, the exponent will be 0. 99.9% of the time, the exponent will lie between -3 and 3. So I read that switch''s can sometimes be optimized into jump tables if the case expressions are nearly contigous integers. So I coded up this little diddy: static inline double mult_power(double x, int y) { switch ((y)) { case 0 : return 1.0; case -3 : return 1.0 / ((x)*(x)*(x)); case -2 : return 1.0 / ((x)*(x)); case -1 : return 1.0 / (x); case 1 : return (x); case 2 : return (x)*(x); case 3 : return (x)*(x)*(x); default : return pow((x), (y)); } } I actually havent tested this vs sticking the 0 in the middle (which I will be doing shortly) but is this a sound replacement?

嗯,有数值方面的考虑因素(即,你的结果不会与仅仅调用pow()相同。)但是你的想法非常好 有动力。确实应该有一个粉末(双x,int y)函数 在C中(甚至更多。)这样做的问题可以快到 整数实际上是一个有趣的。 哦是的,如果编译器 已将其转换为跳转表,则情况的顺序无关紧要。只有在没有这样做的情况下才会这样做,在这种情况下你应该尽可能早地把最多的b $ b b常见案例。

Well, there are numerical considerations (i.e., your results will not be identical to just calling pow().) But your idea is very well motivated. There really should be a powint(double x, int y) function in C (that does even more.) The problem of doing this as quickly as possible for all integers is actually an interesting one. Oh yeah, and the order of the cases should not matter if the compiler has transformed this to a jump table. It only matters in the cases where it doesn''t do that, in which case you should just put the most common cases as early as possible.

或者是否有更有效的解决方案? Or is there perhaps a more efficient solution?

的确如此。如果y大部分为零,那么你应该这样做: #define fastPowInt(x,y)((0 ==(y))?1.0:mult_power((x), (y))) 原因是顶部的单个条件可以利用现代微处理器的分支预测功能,从而降低成本 绝大多数情况下的开销最小。一个 的switch()操作确实在许多编译器中使用了jmp表,但 除非你几乎总是切换到相同的情况,这有 与间接函数调用大致相同的开销(通过 a函数指针调用。)因此,周围的条件如上所示 应该进一步提高性能。 /> - Paul Hsieh www.pobox/~qed/ bstring.sf/

Indeed. If y is mostly zero, then you should do this: #define fastPowInt(x,y) ((0 == (y)) ? 1.0 : mult_power ((x), (y))) The reason is that the single conditional on the top can leverage a modern microprocessor''s branch prediction capabilities, and thus cost the absolutely least overhead for the vast majority of cases. A switch() operation indeed does use jmp tables in many compilers, but unless you are almost always switching to the same case, this has roughly the same overhead as an indirect function call (calling through a function pointer.) So, the surrounding conditional as shown above should improve performance further still. -- Paul Hsieh www.pobox/~qed/ bstring.sf/

ch *********** @ gmail 发布: ch***********@gmail posted: case -3:return 1.0 /((x)*(x)*(x)); case -3 : return 1.0 / ((x)*(x)*(x));

为什么大量使用括号?你不是在写一个宏。 返回1.0 /(x * x * x); 或者可能: 返回1.0 / x / x / x;

Why the copious use of parentheses? You''re not writing a macro. return 1.0 / (x*x*x); Or maybe: return 1.0 / x / x / x;

或者是否有更有效的解决方案? Or is there perhaps a more efficient solution?

Presumbly" pow"应检查整数指数...? - Frederick Gotham

Presumbly "pow" should be checking for integer exponents... ? -- Frederick Gotham

更多推荐

内联电源功能更换

本文发布于:2023-11-09 13:43:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572513.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内联   电源   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!