How can I decrease program processing time?

Asked

Viewed 934 times

3

I need to show this exit:

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

#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!

  • 1

    In case you entered with 28? You can simplify this and then you will probably be + fast.

4 answers

6

It follows 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 number 10 as an example on IDEONE.

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

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

  • I showed the ternary as an alternative to if, but it is worth saying that normally, if the criterion is efficiency, the structure similar to @Maniero’s response is more appropriate, although longer.

Follow the code similar to @Maniero’s by swapping the rest operator for bits:

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 put a demo on IDEONE with very little logic, but in less lines.

5

As the variation is constant of three values and one PMU, print three variables at once and the text (whereas they are always multiples of 4 and a constant variation as shown in the example).

X y z PUM

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

Will get faster!

5


You can simplify a lot, then the performance will possibly be better. The code is very confusing and there is a lot of unnecessary stuff. But you have to measure it to see if it got faster, there are cases that surprise us. A rest calculation may be worse than some additions (for this it may be more interesting to use the operator and which is certainly very fast, as shown by Bacco’s response). 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);
    } 
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2

I made one this way here, using the tips of the answers, greatly improved 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;
}
  • 1

    Oi Thiago, still missing one of the answers, since it was based on more than one, select the one that inspired you best.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.