I want to compare sizeof
and strlen
by process running time, but the result is related with code order. I couldn't understand it , why??
By the way , my pc is using Clion
to run c/c++.
- Fitst:
#include <bits/stdc++.h>
using namespace std;
char p[100000]={"hello,world"};
int main(){
srand(time(NULL));
clock_t start=clock();
printf("size is:%d\n", strlen(p));
printf("strlen() time:%ld\n" ,clock()-start);
start=clock();
printf("size is:%d\n",sizeof(p)/ sizeof(char));
printf("sizeof() time:%ld\n" ,clock()-start);
return 0;
}
- result 1:
size is:11
strlen() time:20
size is:100000
sizeof() time:1
- Second:
#include <bits/stdc++.h>
using namespace std;
char p[100000]={"hello,world"};
int main(){
srand(time(NULL));
clock_t start=clock();
printf("size is:%d\n",sizeof(p)/ sizeof(char));
printf("sizeof() time:%ld\n" ,clock()-start);
start=clock();
printf("size is:%d\n", strlen(p));
printf("strlen() time:%ld\n" ,clock()-start);
return 0;
}
- result2:
size is:100000
sizeof() time:20
size is:11
strlen() time:1