0

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
  • 1
    That's much too short to benchmark like this. And _don't_ do I/O in the part you're measuring (unless you're measuring I/O performance). – Mat Nov 06 '20 at 12:38
  • 4
    Since C++ doesn't have variable-length arrays, `sizeof` is a pure compile-time operator. Its result is calculated by the compiler itself, without any run-time overhead. Besides, `sizeof(char)` is defined b y the C++ specification to always be `1`, so `sizeof(p)/ sizeof(char)` is equal to `100000/1`, which of course always will be `100000`. – Some programmer dude Nov 06 '20 at 12:39
  • 2
    Also please try to unlearn the bad habits you've have. Like [``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), global variables, really large arrays, character arrays for strings, and using `printf` instead of `std::cout`. Even in short simple example code like this, you should use good habits, because all habits, good or bad, tend to stick. So better use good habits and have them stick instead. – Some programmer dude Nov 06 '20 at 12:41
  • don't use `sizeof` to determine the lenght of strings or arrays. Either you know the length (it is 100000 in your case) or `sizeof` won't help anyhow. Use `std::string` for variably sized strings (and `std::vector` for dynamically sized arrays) – 463035818_is_not_a_number Nov 06 '20 at 12:43
  • 2
    You are comparing apples and oranges. `sizeof` is calculated by your compiler. `strlen` is calculated at runtime. And in this case the compiler might optimize it out completely. This comparison is completely meaningless. – Sam Varshavchik Nov 06 '20 at 12:44
  • On another note, your "benchmark" also includes the first output, and part of the second `printf` call. This will actually be the main time-consumer in the code. And probably the reason for the results you're seeing, as the very first call to `printf` could include some initialization of the whole `stdio` system. Try adding a single `printf` before the "benchmark" to see how it handles then. – Some programmer dude Nov 06 '20 at 12:45
  • There is no such thing like C/C++ and the posted code is C++. – Thomas Sablik Nov 06 '20 at 13:24

0 Answers0