Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
C++
#define   SAVE_FACTOR(fact, exp) { if (exp > 0)             \
          factors[count] = fact, \
          exps[count++]  = i;    \

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


Which the efficiency of the two?

[EDIT: Added code tags, separated code#1 & code#2]
Posted
Updated 26-Feb-12 16:33pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Feb-12 20:33pm    
Simply time them. You did not clearly indicate where is code #1 and #2.
One short note: usually ++i is faster then i++. I did not believe before I tried.
--SA

1 solution

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)
 
Share this answer
 
Comments
yuyanci 27-Feb-12 9:04am    
Can I unstand you point in this way .
The code 1 is better than code 2.
sorry ,I am Chinese student ,my English is poor.
I am primer;
enhzflep 27-Feb-12 12:17pm    
Is no problem. Is much better than my Chinese!! :)

Macro(#1) = Fast Speed
Function(#2) = Small Size
yuyanci 27-Feb-12 23:37pm    
which country are in now ?? hava you been china ???
thank you very much you can answer my qustion .thank you !!!
enhzflep 27-Feb-12 23:41pm    
I live in Australia.

Nope, never left this country.

I learned german in school, but do not know any chinese..

Best regards to you sir/maddam! Glad to have helped.
yuyanci 28-Feb-12 19:29pm    
why I use this code fault ??
#define swap(int *p,int *q){int temp;temp=*p;*p=*q;*q=temp;}
the error : error C2010: '*' : unexpected in macro formal parameter list......
#define swap (int x,int y){int temp;temp=x;x=y;y=temp;}is right???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900