Advertisement
sebbu

environment variable access bench

Jun 27th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. inline void loop1(char *buf, clock_t t, int a, int i) {
  14.     t=clock();
  15.     for(a=0;a<LOOP;++a) {
  16.         for(i=0;environ[i]!=NULL;++i) {
  17.             if(strncmp(environ[i],"PATH=",5)==0) buf=environ[i]+5;
  18.         }
  19.     }
  20.     t=clock()-t;
  21.     printf("%s\n", buf);
  22.     printf ("%d clicks (%f seconds)\n", (int)t, ((float)t)/CLOCKS_PER_SEC);
  23. }
  24.  
  25. inline void loop2(char *buf, clock_t t, int a, int i, char* envp[]) {
  26.     t=clock();
  27.     for(a=0;a<LOOP;++a) {
  28.         for(i=0;envp[i]!=NULL;++i) {
  29.             if(strncmp(envp[i],"PATH=",5)==0) buf=envp[i]+5;
  30.         }
  31.     }
  32.     t=clock()-t;
  33.     printf("%s\n", buf);
  34.     printf ("%d clicks (%f seconds)\n", (int)t, ((float)t)/CLOCKS_PER_SEC);
  35. }
  36.  
  37. inline void loop3(char *buf, clock_t t, int a) {
  38.     t=clock();
  39.     for(a=0;a<LOOP;++a) {
  40.         buf=getenv("PATH");
  41.     }
  42.     t=clock()-t;
  43.     printf("%s\n", buf);
  44.     printf ("%d clicks (%f seconds)\n", (int)t, ((float)t)/CLOCKS_PER_SEC);
  45. }
  46.  
  47. int main(int argc, char* argv[], char* envp[]) {
  48.     char *buf=NULL;
  49.     clock_t t=0;
  50.     int a=0;
  51.     int i=0;
  52.     if(argc==2) {
  53.         if( strcmp(argv[1],"-v")==0 || strcmp(argv[1],"--version")==0 ) {
  54.             printf("%s version 0.0.1\n", argv[0]);
  55.         }
  56.         if(argv[1][1]=='\0') {
  57.             if(argv[1][0]=='1') loop1(buf, t, a, i);
  58.             if(argv[1][0]=='2') loop2(buf, t, a, i, envp);
  59.             if(argv[1][0]=='3') loop3(buf, t, a);
  60.         }
  61.         return 1;
  62.     }
  63.     return 0;
  64. }
  65. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement