我想知道的C ++问题效率是最好的 [英] the c++ question efficiency I want to know whitch is the best

查看:56
本文介绍了我想知道的C ++问题效率是最好的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include  <stdio.h>
#include  <stdlib.h>

#define   MAXSIZE  100
#define   SAVE_FACTOR(fact, exp) { if (exp > 0)             \
                                        factors[count] = fact, \
                                        exps[count++]  = i;    \
                                 }

void main(void)
{
     unsigned long  factors[MAXSIZE]; /* stores factors   */
     unsigned long  exps[MAXSIZE];    /* stores exps      */
     unsigned long  n, work;
     int            count = 0;        /* factor counter   */
     int            i, k;
     char           line[100], *dummy;

     printf("\nFactorization by Division Program");
     printf("\n=================================");
     printf("\n\nInput a positive integer --> ");
     gets(line);
     n = strtoul(line, &dummy, 10);

     for (i=0,work=n; (work & 0x01UL)==0 && work>1; work>>=1,i++)
          ;                   /* extract divisor 2        */
     SAVE_FACTOR(2, i);       /* save it and its exp.     */

     for (k = 3; k <= work; k += 2) { /* for k=3,5,7,9,.. */
          for (i = 0; work % k == 0 && work > 1; work /= k, i++)
               ;              /* extract divisor k        */
          SAVE_FACTOR(k, i);  /* save it and its exp.     */
     }

     printf("\n%ld = ", n);   /* display result.          */
     for (i = 0; i < count; i++)
          printf("%ld(%ld)", factors[i], exps[i]);
}




我想回答功能




I want to answer the fuction

#define   SAVE_FACTOR(fact, exp) { if (exp > 0)             \
          factors[count] = fact, \
          exps[count++]  = i;    \


代替:


Instead of:

void SAVE_FACTOR (fact,exp)
{ 
    if (exp>0) 
    {
        factors[count]=fact;
        exps[count++]=i;
    }
}



两者效率最高?



Which the efficiency of the two?

推荐答案

效率如何?

如果大小合适,则更有效的方法是将SAVE_FACTOR保留为函数.

另一方面,如果您对执行时间有更多的了解,那么(在大多数情况下)将代码用作宏会更快,因为您不必先设置然后清理堆栈并处理执行子例程所需的调用/退出指令.


例如,考虑执行一百万次的循环.

在第一种情况下,您可以删除循环结构并在循环内重复代码1,000,000次.因为我们不处理维护迭代计数,不检查该计数,也不跳回到循环的开始,所以这将稍微快一些.此代码将很大. (但速度稍快-尽管这不允许缓存大小等)

在第二种情况下,此代码将保留在执行一百万次的循环中.该代码将缩小一百万倍.它也会变得稍微慢一些.

想象一下,循环中只包含一条NOP(无操作)指令.第一个实例将意味着1,000,000字节.而第二个意味着只有1个字节.
可以想象,随着循环变得越来越大,越来越有用,在内存和磁盘空间方面,重复执行1,000,000次代码的代价越来越大.您还将面临这种循环"将不再完全适合缓存的可能性.这将导致高速缓存未命中,这再次减慢了执行速度.

循环展开(正如我在这里所解释的)和内联宏(据我了解,您要说的)是应根据具体情况进行评估的内容-尽管仅当尝试挤压每盎司时CPU的功耗,因为通常情况下,增益微不足道.

当条件需要使用时,通常会进行此类优化.通常,通过查看和改进所使用的算法,而不是展开或内联代码,通常可以获得更大的性能提升. (编译器曾经是简单得多的生物)
Efficiency in terms of what?

If it''s size, the more efficient approach would be to keep SAVE_FACTOR as a function.

On the other hand, if you''re more concenrned about execution times, then (in the vast majority of cases) using the code as a macro will be faster, since you don''t have to setup then clean-up the stack and handle the call/ret instructions needed to execute a subroutine.


As an example, consider a loop that executes something a million times.

In the first instance, you may remove the loop construct and repeat the code inside the loop 1,000,000 times. This will be marginally quicker since we''re not dealing with maintaining an iteration count, nor checking this count, nor jumping back to the start of the loop. THIS CODE WILL BE LARGE. (but slightly quicker - although this is not allowing for cache size etc)

In the second instance, this code will remain inside a loop that''s executed a million time. This code will be smaller by a factor of a million. It will also be ever so slightly slower.

Imagine the loop contained nothing but a NOP (no-operation) instruction. The first instance would mean 1,000,000 bytes. While the second would mean just 1 byte.
As you can imagine, as the loop grows larger and more useful the penalty of repeating the code 1,000,000 times becomes greater and greater - both in terms of memory and disk-space. You also face the possibility that this ''loop'' will no longer fit entirely into cache. This will cause cache-misses, which again slow down execution.

Loop-unrolling (as I''ve explained here) and inline-macros (as I understand you to be speaking of) are something that should be evaluated on a case-by-case basis - though only when trying to squeeze every last ounce of power out of a cpu, since often the gains are negligible to small.

Optimizations like this are oft left to when conditions require their use. Much larger performance gains may usually be had by reviewing & improving the algorithm used, rather than unrolling or inlining code. (Compilers used to be far simpler creatures)


这篇关于我想知道的C ++问题效率是最好的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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