Concatenating strings in C, which method is more efficient?

后端 未结 10 916
迷失自我
迷失自我 2020-11-28 19:51

I came across these two methods to concatenate strings:

Common part:

char* first= \"First\";
char* second = \"Second\";
char* both = malloc(strlen(fi         


        
10条回答
  •  独厮守ぢ
    2020-11-28 20:44

    Here's some madness for you, I actually went and measured it. Bloody hell, imagine that. I think I got some meaningful results.

    I used a dual core P4, running Windows, using mingw gcc 4.4, building with "gcc foo.c -o foo.exe -std=c99 -Wall -O2".

    I tested method 1 and method 2 from the original post. Initially kept the malloc outside the benchmark loop. Method 1 was 48 times faster than method 2. Bizarrely, removing -O2 from the build command made the resulting exe 30% faster (haven't investigated why yet).

    Then I added a malloc and free inside the loop. That slowed down method 1 by a factor of 4.4. Method 2 slowed down by a factor of 1.1.

    So, malloc + strlen + free DO NOT dominate the profile enough to make avoiding sprintf worth while.

    Here's the code I used (apart from the loops were implemented with < instead of != but that broke the HTML rendering of this post):

    void a(char *first, char *second, char *both)
    {
        for (int i = 0; i != 1000000 * 48; i++)
        {
            strcpy(both, first);
            strcat(both, " ");
            strcat(both, second);
        }
    }
    
    void b(char *first, char *second, char *both)
    {
        for (int i = 0; i != 1000000 * 1; i++)
            sprintf(both, "%s %s", first, second);
    }
    
    int main(void)
    {
        char* first= "First";
        char* second = "Second";
        char* both = (char*) malloc((strlen(first) + strlen(second) + 2) * sizeof(char));
    
        // Takes 3.7 sec with optimisations, 2.7 sec WITHOUT optimisations!
        a(first, second, both);
    
        // Takes 3.7 sec with or without optimisations
        //b(first, second, both);
    
        return 0;
    }
    

提交回复
热议问题