Created
May 26, 2012 00:46
-
-
Save orangejulius/2791520 to your computer and use it in GitHub Desktop.
strlen performance comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A small pair of C programs to answer pepe-ramollo's question at www.reddit.com/r/C_Programming/comments/u53xv/i_dont_get_the_implementation_of_strlen/ | |
use make test to run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: mystrlen strlen | |
mystrlen: mystrlen.c makestrs.h | |
gcc -O2 mystrlen.c -o mystrlen | |
strlen: strlen.c makestrs.h | |
gcc -O2 strlen.c -o strlen | |
clean: | |
rm mystrlen strlen | |
test: mystrlen strlen | |
time ./mystrlen > /dev/null | |
time ./strlen > /dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#define NUMSTRS 40000 | |
char** makestrs() | |
{ | |
char **strlist = malloc(sizeof(char*) * NUMSTRS); | |
int i; | |
//create NUMSTRS strings pointed to by array elements of strlist | |
//for simplicity, the strings are all filled with 'a' except for the final null byte | |
//additionally, they increase in size from 1 to NUMSTRS+1 | |
for (i = 0; i < NUMSTRS; i++) { | |
strlist[i] = malloc(sizeof(char) * (i+1)); | |
int j; | |
for (j = 0; j < i+1; j++) { | |
if (j == i) { | |
strlist[i][j] = '\0'; | |
} else { | |
strlist[i][j] = 'a'; | |
} | |
} | |
//printf("string %d is %s (size %d)\n", i, strlist[i], strlen(strlist[i])); | |
} | |
return strlist; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//test the performance of the naive implementation | |
#include <stdlib.h> | |
#include "makestrs.h" | |
size_t mystrlen (const char *str) | |
{ | |
size_t len = 0; | |
while (str[len]!='\0'){ | |
len++; | |
} | |
return len; | |
} | |
int main() | |
{ | |
char **strings = makestrs(); | |
int i; | |
for (i = 0; i < NUMSTRS; i++) { | |
printf("%ld\n",mystrlen(strings[i])); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//test the performance of the naive implementation | |
#include <string.h> | |
#include "makestrs.h" | |
int main() | |
{ | |
char **strings = makestrs(); | |
int i; | |
for (i = 0; i < NUMSTRS; i++) { | |
printf("%ld\n", strlen(strings[i])); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment