Advertisement
sebbu

environment variable access bench

Jun 27th, 2014
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <cassert>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. //#include <iostream>
  6. #include <cstring>
  7. //#include <string>
  8.  
  9. extern char ** environ;
  10.  
  11. const int LOOP=100000;
  12.  
  13. int main(int argc, char* argv[], char* envp[]) {
  14.     if(argc==2) {
  15.         if( strcmp(argv[1],"-v")==0 || strcmp(argv[1],"--version")==0 ) {
  16.             printf("%s version 0.0.1\n", argv[0]);
  17.         }
  18.         return 1;
  19.     }
  20.     char *buf;
  21.     clock_t t=clock();
  22.     for(int a=0;a<LOOP;++a) {
  23.         for(int i=0;environ[i]!=NULL;++i) {
  24.             if(strncmp(environ[i],"PATH=",5)==0) buf=environ[i]+5;
  25.         }
  26.     }
  27.     t=clock()-t;
  28.     printf("%s\n", buf);
  29.     printf ("%d clicks (%f seconds)\n", (int)t, ((float)t)/CLOCKS_PER_SEC);
  30.     t=clock();
  31.     for(int a=0;a<LOOP;++a) {
  32.         for(int i=0;envp[i]!=NULL;++i) {
  33.             if(strncmp(envp[i],"PATH=",5)==0) buf=envp[i]+5;
  34.         }
  35.     }
  36.     t=clock()-t;
  37.     printf("%s\n", buf);
  38.     printf ("%d clicks (%f seconds)\n", (int)t, ((float)t)/CLOCKS_PER_SEC);
  39.     t=clock();
  40.     for(int a=0;a<LOOP;++a) {
  41.         buf=getenv("PATH");
  42.     }
  43.     t=clock()-t;
  44.     printf("%s\n", buf);
  45.     printf ("%d clicks (%f seconds)\n", (int)t, ((float)t)/CLOCKS_PER_SEC);
  46.     return 0;
  47. }
  48. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement