How can I decrease the processing time of the program?

3

I need to display this output:

1 2 3 PUM
5 6 7 PUM
9 10 11 PUM
13 14 15 PUM
17 18 19 PUM
21 22 23 PUM
25 26 27 PUM

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

int main(){
    unsigned short int in, i = 0, j = 1, k = 0;

     scanf("%hd", &in);

    while(i < in){
        while(k < 3){
            printf("%hd ", j);
            j++;
            k++;
        }

      j += 1;
      k = 0;
      i++;
      printf("PUM\n");

     } 

return 0;
}

I accept optimization suggestions!

    
asked by anonymous 03.07.2016 / 01:02

4 answers

5

It's pretty easy to do, so the performance will probably be better. The code is pretty confusing and has a lot of unnecessary stuff. But you have to measure to see if it has gotten faster, there are cases that surprises us. A rest calculation may be worse than some additions (for this it may be more interesting to use the and operator which is certainly very fast, as Bacco's answer shows). Using a type int can give a gain if the architecture is optimized for this type.

#include <stdio.h>

int main(){
    int in;
    scanf("%d", &in);
    for (int i = 1; i <= in; i++) {
        if (i % 4 == 0) {
            printf("PUM\n");
        } else {
            printf("%d ", i);
        }
    } 
    return 0;
}

See running on ideone and on CodingGround .

Alternative based on Leo's response:

#include<stdio.h>

int main(){
    int in;
    scanf("%d", &in);
    for (int i = 1; i <= in; i+=4) {
        printf("%d %d %d PUM\n", i, i + 1, i + 2);
    } 
    return 0;
}

See working on ideone and on CodingGround .

    
03.07.2016 / 01:33
6

There is an alternative as short as Thiago's answer, but it works with numbers that are not multiples of 4:

int main(){
    unsigned short int in, i;

    scanf("%hd", &in);

    for( i = 1; i <= in; i++ ) printf( i & 3 ? "%d " : "PUM\n", i );
    return 0;
}

See working with the number 10 as an example in IDEONE .

    The i & 3 operation is a quick way to get the 2 least significant bits of the counter, effectively returning 0 for all cases where the "PUM" message should be displayed.

    / li>
  • The ternary operator will use "%d " in all cases where the mentioned expression does not return 0

  • I have shown the ternary as an alternative to if , but it is worth saying that normally, if the criterion is efficiency, the structure similar to the @bigown response is more appropriate, though longer.

    / li>

Follow the code similar to @bigown by changing the rest operator by bits operation:

int main(){
    int in, i;
    scanf("%d", &in);
    for ( i = 1; i <= in; i++ ) {
        if (i & 3) {
            printf("%d ", i);
        } else {
            printf("PUM\n");
        }
    } 
    return 0;
}

I've put a demo in IDEONE with the same logic but in less lines.

    
03.07.2016 / 05:07
5

Since the variation is a constant of three values and a PUM, print three variables at a time and the text (assuming they are always multiples of 4 and a constant variation as shown in the example).

x and z PUM

After the PUM, on the next loop, add four to the value of each variable.

It will get faster!

    
03.07.2016 / 01:30
2

I made this one here, using the tips of the answers, it improved a lot from my first code for this kkk

 int main(){
    unsigned short int in, i = 1;

    scanf("%hd", &in);

    while(i <= in*4){
         printf("%d %d %d PUM\n", i, i + 1, i + 2);
         i+=4;
    }  

    return 0;
}
    
03.07.2016 / 04:49