Advertisement
Guest User

Untitled

a guest
Jul 30th, 2010
2,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <cctype>
  8. using namespace std;
  9.  
  10. const int N=10000000,NC=10*N;
  11. int a[N],swtime,swsize;
  12. char s[NC];
  13. char sw[16*N];
  14.  
  15. void gen() {
  16.     for (int i=0;i<N;i++) {
  17.         a[i]=rand();
  18.     }
  19.     for (int i=0;i<NC;i++) {
  20.         s[i]=rand()%10+'0';
  21.     }
  22.     int cur=clock();
  23.     swsize=0;
  24.     for (int i=0;i<N;i++) {
  25.         char s[100];
  26.         sprintf(s,"%d",a[i]);
  27.         for (int j=0;s[j];j++) {
  28.             sw[swsize++]=s[j];
  29.         }
  30.         sw[swsize++]=' ';
  31.     }
  32.     swtime=clock()-cur;
  33. }
  34.  
  35. void test_printf_int() {
  36.     int cur=clock();
  37.     freopen("temp.txt","w",stdout);
  38.     for (int i=0;i<N;i++)
  39.         printf("%d ",a[i]);
  40.     fclose(stdout);
  41.     fprintf(stderr,"Test: (printf, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
  42. }
  43.  
  44. void test_cout_int() {
  45.     int cur=clock();
  46.     freopen("temp.txt","w",stdout);
  47.     for (int i=0;i<N;i++)
  48.         cout << a[i] << ' ';
  49.     fclose(stdout);
  50.     fprintf(stderr,"Test: (cout, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
  51. }
  52.  
  53. void test_write_int() {
  54.     int cur=clock();
  55.     freopen("temp.txt","w",stdout);
  56.     cout.write(sw,swsize);
  57.     fclose(stdout);
  58.     fprintf(stderr,"Test: (write, %d ints, %d chars) %.2lfc + %.2lfc\n",N,swsize,(swtime+.0)/CLOCKS_PER_SEC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  59. }
  60.  
  61. void test_fwrite_int() {
  62.     int cur=clock();
  63.     freopen("temp.txt","w",stdout);
  64.     fwrite(sw,1,swsize,stdout);
  65.     fclose(stdout);
  66.     fprintf(stderr,"Test: (fwrite, %d ints, %d chars) %.2lfc + %.2lfc\n",N,swsize,(swtime+.0)/CLOCKS_PER_SEC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  67. }
  68.  
  69. void test_scanf_int() {
  70.     int cur=clock();
  71.     freopen("temp.txt","r",stdin);
  72.     for (int i=0;i<N;i++) {
  73.         int t;
  74.         scanf("%d",&t);
  75.         if (t!=a[i]) {
  76.             fprintf(stderr,"Fail with scanf\n");
  77.             exit(1);
  78.         }
  79.     }
  80.     fclose(stdin);
  81.     fprintf(stderr,"Test: (scanf, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
  82. }
  83.  
  84. void test_cin_int() {
  85.     int cur=clock();
  86.     freopen("temp.txt","r",stdin);
  87.     for (int i=0;i<N;i++) {
  88.         int t;
  89.         cin >> t;
  90.         if (t!=a[i]) {
  91.             fprintf(stderr,"Fail with cin\n");
  92.             exit(1);
  93.         }
  94.     }
  95.     fclose(stdin);
  96.     fprintf(stderr,"Test: (cin, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
  97. }
  98.  
  99. void test_printf_char() {
  100.     int cur=clock();
  101.     freopen("temp.txt","w",stdout);
  102.     for (int i=0;i<NC;i++)
  103.         printf("%c",s[i]);
  104.     fclose(stdout);
  105.     fprintf(stderr,"Test: (printf, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  106. }
  107.  
  108. void test_cout_char() {
  109.     int cur=clock();
  110.     freopen("temp.txt","w",stdout);
  111.     for (int i=0;i<NC;i++)
  112.         cout << s[i];
  113.     fclose(stdout);
  114.     fprintf(stderr,"Test: (cout, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  115. }
  116.  
  117. void test_putchar_char() {
  118.     int cur=clock();
  119.     freopen("temp.txt","w",stdout);
  120.     for (int i=0;i<NC;i++)
  121.         putchar(s[i]);
  122.     fclose(stdout);
  123.     fprintf(stderr,"Test: (putchar, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  124. }
  125.  
  126. void test_scanf_char() {
  127.     int cur=clock();
  128.     freopen("temp.txt","r",stdin);
  129.     for (int i=0;i<NC;i++) {
  130.         char t;
  131.         scanf("%c",&t);
  132.         if (t!=s[i]) {
  133.             fprintf(stderr,"Fail with scanf\n");
  134.             exit(1);
  135.         }
  136.     }
  137.     fclose(stdin);
  138.     fprintf(stderr,"Test: (scanf, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  139. }
  140.  
  141. void test_cin_char() {
  142.     int cur=clock();
  143.     freopen("temp.txt","r",stdin);
  144.     for (int i=0;i<NC;i++) {
  145.         char t;
  146.         cin >> t;
  147.         if (t!=s[i]) {
  148.             fprintf(stderr,"Fail with cin\n");
  149.             exit(1);
  150.         }
  151.     }
  152.     fclose(stdin);
  153.     fprintf(stderr,"Test: (cin, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  154. }
  155.  
  156. void test_getchar_char() {
  157.     int cur=clock();
  158.     freopen("temp.txt","r",stdin);
  159.     for (int i=0;i<NC;i++) {
  160.         char t=getchar();
  161.         if (t!=s[i]) {
  162.             fprintf(stderr,"Fail with getchar\n");
  163.             exit(1);
  164.         }
  165.     }
  166.     fclose(stdin);
  167.     fprintf(stderr,"Test: (getchar, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
  168. }
  169.  
  170. void test_read_char() {
  171.     int cur=clock();
  172.     freopen("temp.txt","r",stdin);
  173.     cin.read(sw,NC);
  174.     fclose(stdin);
  175.     int cur2=clock();
  176.     for (int i=0;i<NC;i++)
  177.         if (s[i]!=sw[i]) {
  178.             cerr << i << ' ' << (int)s[i] << ' ' << (int)sw[i] << endl;
  179.             fprintf(stderr,"Fail with read\n");
  180.             exit(1);
  181.         }
  182.     fprintf(stderr,"Test: (read, %d chars) %.2lfc + %.2lfc\n",NC,(cur2-cur+.0)/CLOCKS_PER_SEC,(clock()-cur2+.0)/CLOCKS_PER_SEC);
  183. }
  184.  
  185. void test_fread_char() {
  186.     int cur=clock();
  187.     freopen("temp.txt","r",stdin);
  188.     fread(sw,1,NC,stdin);
  189.     fclose(stdin);
  190.     int cur2=clock();
  191.     for (int i=0;i<NC;i++)
  192.         if (s[i]!=sw[i]) {
  193.             fprintf(stderr,"Fail with fread\n");
  194.             exit(1);
  195.         }
  196.     fprintf(stderr,"Test: (fread, %d chars) %.2lfc + %.2lfc\n",NC,(cur2-cur+.0)/CLOCKS_PER_SEC,(clock()-cur2+.0)/CLOCKS_PER_SEC);
  197. }
  198.  
  199. int main () {
  200.     gen();
  201.     test_printf_int();
  202.     test_cout_int();
  203.     test_write_int();
  204.     test_scanf_int();
  205.     test_fwrite_int();
  206.     test_cin_int();
  207.     test_printf_char();
  208.     test_scanf_char();
  209.     test_cout_char();
  210.     test_cin_char();
  211.     test_putchar_char();
  212.     test_getchar_char();
  213.     test_read_char();
  214.     test_fread_char();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement