简体   繁体   English

优化C代码,霍纳多项式求值

[英]Optimizing C code, Horner's polynomial evaluation

I'm trying to learn how to optimize code (I'm also learning C), and in one of my books there's a problem for optimizing Horner's method for evaluation polynomials. 我正在尝试学习如何优化代码(我也在学习C),并且在我的一本书中,存在一个优化霍纳方法用于评估多项式的​​问题。 I'm a little lost on how to approach the problem. 我对如何解决这个问题有些迷惑。 I'm not great at recognizing what needs optimizing. 我不太擅长识别需要优化的内容。

Any advice on how to make this function run faster would be appreciated. 任何有关如何使此功能运行更快的建议,将不胜感激。

Thanks 谢谢

 double polyh(double a[], double x, int degree) {
        long int i;
        double result = a[degree];
        for (i = degree-1; i >= 0; i--) 
            result = a[i] + x*result;
        return result;
    }

You really need to profile your code to test whether proposed optimizations really help. 您确实需要分析您的代码,以测试建议的优化是否真正有用。 For example, it may be the case that declaring i as long int rather than int slows the function on your machine, but on the other hand it may make no difference on your machine but might make a difference on others, etc. Anyway, there's no reason to declare i a long int when degree is an int , so changing it probably won't hurt. 例如,在某些情况下,将i声明为long int而不是int会减慢计算机的功能,但另一方面,这可能对您的计算机没有任何影响,但可能对其他计算机有所影响,依此类推。当degree是一个int ,没有理由将i声明为long int ,因此更改它可能不会受到伤害。 (But still profile!) (但仍是个人资料!)

Horner's rule is supposedly optimal in terms of the number of multiplies and adds required to evaluate a polynomial, so I don't see much you can do with it. 霍纳法则在评估多项式所需的乘法和加法数方面被认为是最佳的,因此我认为您无法对此做很多事情。 One thing that might help (profile!) is changing the test i>=0 to i!=0 . 可能有帮助(配置文件!)的一件事是将测试i>=0更改为i!=0 Of course, then the loop doesn't run enough times, so you'll have to add a line below the loop to take care of the final case. 当然,循环不会运行足够的时间,因此您必须在循环下方添加一行以处理最后的情况。

Alternatively you could use a do { ... } while (--i) construct. 另外,您可以使用do { ... } while (--i)构造。 (Or is it do { ... } while (i--) ? You figure it out.) (或者是do { ... } while (i--)吗?您知道了。)

You might not even need i , but using degree instead will likely not save an observable amount of time and will make the code harder to debug, so it's not worth it. 您甚至可能不需要i ,但是使用degree可能不会节省可观察的时间,并使代码更难调试,因此不值得。

Another thing that might help (I doubt it, but profile!) is breaking up the arithmetic expression inside the loop and playing around with order, like 可能有帮助的另一件事(我对此表示怀疑,但配置文件!)是破坏循环内的算术表达式并按顺序进行操作,例如

for (...) {
  result *= x;
  result += a[i];
}

which may reduce the need for temporary variables/registers. 这可以减少对临时变量/寄存器的需求。 Try it out. 试试看。

Some suggestion: 一些建议:

  1. You may use int instead of long int for looping index. 您可以使用int代替long int来循环索引。

Almost certainly the problem is inviting you to conjecture on the values of a . 几乎可以肯定,问题是邀请您对a的值进行猜测。 If that vector is mostly zeros, then you'll go faster (by doing fewer double multiplications, which will be the clear bottleneck on most machines) by computing only the values of a[i] * x^i for a[i] != 0 . 如果该向量大部分为零,那么通过仅计算a[i] * x^ia[i] * x^i值,您会更快(通过执行较少的double乘法,这将是大多数机器上的明显瓶颈) a[i] != 0 In turn the x^i values can be computed by careful repeated squaring, preserving intermediate terms so that you never compute the same partial power more than once. 依次, x^i值可以通过仔细地重复平方来计算,保留中间项,这​​样您就永远不会多次计算相同的分幂。 See the Wikipedia article if you've never implemented repeated squaring. 如果您从未实现过重复平方, 请参阅Wikipedia文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM