Thread: I created two programs and I do not know which is faster

  1. #1
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42

    I created two programs and I do not know which is faster

    I have two generators of wps codes:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int num=0;
    	for(;num<99999999;++num)
    	{
    		if(num<10)
    		    fprintf(stdout, "0000000%d\n", num);
    		else if(num<100)
    		    fprintf(stdout,"000000%d\n", num);
    		else if(num<1000)
    		    fprintf(stdout, "00000%d\n", num);
    		else if(num<10000)
    		    fprintf(stdout, "0000%d\n", num);
    		else if(num<100000)
    		    fprintf(stdout, "000%d\n", num);
    		else if(num<1000000)
    		    fprintf(stdout, "00%d\n", num);
    		else if(num<10000000)
    		    fprintf(stdout, "0%d\n", num);
    	}
    }
    Two:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char str[11];
    	double num=0;
    	for(int i=0; i<=99999999; i++)
    	{
    	    snprintf(str, 11, "%.8f", num);
    	    for(int a=2; a<=11; a++)
    	         putchar(str[a]);
            putchar('\n');
    	    num+=0.0000001;
    	}
    }
    The second code on my phone works faster.
    I'm looking for volunteers who measure the speed of execution of these two codes and write here about the results. I make the program and I need the fastest solution, so I ask for your help!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    a[11] is outside the bounds of the array. You should know that by now.

    In the first program, you don't need the if/else if stuff. Just do this:
    Code:
                printf("%08d\n", num);
    The 8 is the field width, and the 0 before it says to pad the value with zeroes instead of spaces.

    At any rate, it's probably somewhat pointless to worry about the difference in speed between these two programs since they are presumably both wasting the VAST majority of their time in the print statments.

    If you really had to speed it up as much as possible, then maybe something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SIZE 8
    
    int main() {
        char s[SIZE+1] = {0};
        memset(s, '0', SIZE);
    
        for (;;) {
            fputs(s, stdout);
            putchar('\n');
    
            int i = SIZE - 1;
            while (i >= 0 && s[i] == '9')
                s[i--] = '0';
    
            if (i < 0)
                break;
    
            s[i]++;
        }
    
        return 0;
    }

  3. #3
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by algorism View Post
    a[11] is outside the bounds of the array. You should know that by now.
    Of course I know this, but the code works as read carefully the code, it says a<= and not a<. Running the code gives a stable generation, at least for me, so here you are wrong.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Don't confuse 'works' with 'bug free'.
    It might 'work' for you today, but you can be sure that if you repeatedly assume stepping off the end of the array is benign, then you're in for pain at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    I made a harvester of 3 programs:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    #define SIZE 8
    
    #define MAX1 100000
    #define MAX2 100000
    #define MAX3 100000
    
    int main()
    {
    	int start=time(NULL);
    	for(int num=0; num<MAX1;++num)
    	{
    		printf("Generator 1: ");
    		printf("%08d\n", num);
        }
        int end=time(NULL);
        int result1=end-start;
        
        start=time(NULL);
        char str[11];
    	double num=0;
    	for(int i=0; i<=MAX2; i++)
    	{
    	    snprintf(str, 11, "%.8f", num);
    	    for(int a=2; a<=10; a++)
    	         putchar(str[a]);
            putchar('\n');
            printf("Generator 2: ");
    	    num+=0.00000001;
    	}
    	end=time(NULL);
    	int result2=end-start;
    	
    	start=time(NULL);
    	char s[SIZE+1] = {0};
        memset(s, '0', SIZE);
        int cnt=0;
        for (;;) {
            fputs(s, stdout);
            putchar('\n');
            printf("Generator 3: ");
    
            int i = SIZE - 1;
            while (i >= 0 && s[i] == '9')
                s[i--] = '0';
    
            if (cnt > MAX3)
                break;
    
            s[i]++;
            cnt++;
        }
        end=time(NULL);
    	int result3=end-start;
    	
    	printf("\n\nGenerator 1: %d seconds\n", result1);
    	printf("Generator 2: %d seconds\n", result3);
    	printf("Generator 3: %d seconds\n", result3);
    }
    Performance gave me the following results:
    Generator 1: 8 seconds
    Generator 1: 7 seconds
    Generator 1: 7 seconds

    The following code is available:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    #define SIZE 8
    
    #define MAX1 99999999
    #define MAX2 99999999
    #define MAX3 99999999
    
    int main()
    {
    	int start=time(NULL);
    	for(int num=0; num<MAX1;++num)
    	{
    		printf("Generator 1: ");
    		printf("%08d\n", num);
        }
        int end=time(NULL);
        int result1=end-start;
        
        start=time(NULL);
        char str[11];
    	double num=0;
    	for(int i=0; i<=MAX2; i++)
    	{
    	    snprintf(str, 11, "%.8f", num);
    	    for(int a=2; a<=10; a++)
    	         putchar(str[a]);
            putchar('\n');
            printf("Generator 2: ");
    	    num+=0.00000001;
    	}
    	end=time(NULL);
    	int result2=end-start;
    	
    	start=time(NULL);
    	char s[SIZE+1] = {0};
        memset(s, '0', SIZE);
        int cnt=0;
        for (;;) {
            fputs(s, stdout);
            putchar('\n');
            printf("Generator 3: ");
    
            int i = SIZE - 1;
            while (i >= 0 && s[i] == '9')
                s[i--] = '0';
    
            if (cnt > MAX3)
                break;
    
            s[i]++;
            cnt++;
        }
        end=time(NULL);
    	int result3=end-start;
    	
    	printf("\n\nGenerator 1: %d seconds\n", result1);
    	printf("Generator 2: %d seconds\n", result3);
    	printf("Generator 3: %d seconds\n", result3);
    }
    Who can execute this code and report the results?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:35:6: warning: unused variable ‘result2’ [-Wunused-variable]
    int result2=end-start;
    ^
    Mmm, you print the same result twice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Door number 3 please Bob.
    Code:
    $ gcc -Wall -O2 bar.c
    $ ./a.out | tail -3
    Generator 1: 11 seconds
    Generator 2: 34 seconds
    Generator 3: 5 seconds
    $ gcc -Wall bar.c
    $ ./a.out | tail -3
    Generator 1: 11 seconds
    Generator 2: 33 seconds
    Generator 3: 6 seconds
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by algorism View Post
    a[11] is outside the bounds of the array. You should know that by now.

    In the first program, you don't need the if/else if stuff. Just do this:
    Code:
                printf("%08d\n", num);
    The 8 is the field width, and the 0 before it says to pad the value with zeroes instead of spaces.

    At any rate, it's probably somewhat pointless to worry about the difference in speed between these two programs since they are presumably both wasting the VAST majority of their time in the print statments.

    If you really had to speed it up as much as possible, then maybe something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SIZE 8
    
    int main() {
        char s[SIZE+1] = {0};
        memset(s, '0', SIZE);
    
        for (;;) {
            fputs(s, stdout);
            putchar('\n');
    
            int i = SIZE - 1;
            while (i >= 0 && s[i] == '9')
                s[i--] = '0';
    
            if (i < 0)
                break;
    
            s[i]++;
        }
    
        return 0;
    }
    Quote Originally Posted by Salem View Post
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:35:6: warning: unused variable ‘result2’ [-Wunused-variable]
    int result2=end-start;
    ^
    Mmm, you print the same result twice.
    There's a small mistake, here's the correct code
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    #define SIZE 8
    
    #define MAX1 100000
    #define MAX2 100000
    #define MAX3 100000
    
    int main()
    {
    	int start=time(NULL);
    	for(int num=0; num<MAX1;++num)
    	{
    		printf("Generator 1: ");
    		printf("%08d\n", num);
        }
        int end=time(NULL);
        int result1=end-start;
        
        start=time(NULL);
        char str[11];
    	double num=0;
    	for(int i=0; i<=MAX2; i++)
    	{
    	    snprintf(str, 11, "%.8f", num);
    	    for(int a=2; a<=10; a++)
    	         putchar(str[a]);
            putchar('\n');
            printf("Generator 2: ");
    	    num+=0.00000001;
    	}
    	end=time(NULL);
    	int result2=end-start;
    	
    	start=time(NULL);
    	char s[SIZE+1] = {0};
        memset(s, '0', SIZE);
        int cnt=0;
        for (;;) {
            fputs(s, stdout);
            putchar('\n');
            printf("Generator 3: ");
    
            int i = SIZE - 1;
            while (i >= 0 && s[i] == '9')
                s[i--] = '0';
    
            if (cnt > MAX3)
                break;
    
            s[i]++;
            cnt++;
        }
        end=time(NULL);
    	int result3=end-start;
    	
    	printf("\n\nGenerator 1: %d seconds\n", result1);
    	printf("Generator 2: %d seconds\n", result2);
    	printf("Generator 3: %d seconds\n", result3);
    }

  9. #9
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by Salem View Post
    Door number 3 please Bob.
    Code:
    $ gcc -Wall -O2 bar.c
    $ ./a.out | tail -3
    Generator 1: 11 seconds
    Generator 2: 34 seconds
    Generator 3: 5 seconds
    $ gcc -Wall bar.c
    $ ./a.out | tail -3
    Generator 1: 11 seconds
    Generator 2: 33 seconds
    Generator 3: 6 seconds
    I realized why the code runs so slowly. I made this code:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	FILE *f;
    	f = freopen("out", "a", stdout);
    	for(int num=0; num<1000000;++num)
    		printf("%08d\n", num);
    	fclose(f);
    }
    For testing, I ran like this: time ./gen
    The result is 2 seconds every time you start. But maybe there is an even faster solution, it's sad if you insert a tweak generator. But on my processor MTK6572 this is a very high speed.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What?!?!?!?!?!?!?

    What do you mean that you "realized why the code runs so slowly"? All you've done is decreased the number of iterations by a factor of 100!!! How is that a fair way to speed it up?

    The code I wrote was designed to be faster than the code you wrote. If you think about it, it's obvious why it's faster. It completely bypasses any need to turn a binary value into decimal character output since it deals with the character representation directly.
    Last edited by algorism; 07-16-2017 at 07:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is a buffer created here?
    By stariz in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2012, 09:21 AM
  2. Replies: 1
    Last Post: 11-09-2009, 07:03 AM
  3. Some help with make my programs faster
    By Sshakey6791 in forum C++ Programming
    Replies: 11
    Last Post: 12-11-2008, 01:41 PM
  4. How are MIDIs created?
    By Stan100 in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2004, 01:00 PM
  5. how are wizards normally created?
    By endo in forum Windows Programming
    Replies: 1
    Last Post: 07-25-2002, 05:12 AM

Tags for this Thread