fork(1) download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef unsigned int uint32;
  6.  
  7. const int buf_size = 1024 * 1024 * 1024 / 4;
  8. typedef int buf_type;
  9. volatile int read_hack;
  10.  
  11. void test4write( buf_type *buf )
  12. {
  13. for ( int i = 0; i < buf_size; i++ )
  14. {
  15. buf[ i ] = 0;
  16. };
  17. };
  18.  
  19. void test4read( buf_type *buf )
  20. {
  21. buf_type res = 0;
  22. for ( int i = 0; i < buf_size; i++ )
  23. {
  24. res += buf[ i ];
  25. };
  26. read_hack = res;
  27. //std::cout << "res: " << res << "\n";
  28. };
  29.  
  30. typedef void (*fptr)(buf_type *);
  31.  
  32. void test( fptr fp, const char *label, buf_type *param )
  33. {
  34. clock_t t = clock();
  35. fp( param );
  36. t = clock() - t;
  37. double x = ((double)t) / CLOCKS_PER_SEC;
  38. std::cout << label << " time: " << x << " sec => " << 1/x << " Gb/sec\n";
  39. };
  40.  
  41. int main()
  42. {
  43. buf_type *buf = new buf_type[ buf_size ];
  44. test( test4write, "init write", buf );
  45. test( test4write, "write", buf );
  46. test( test4read, "read", buf );
  47. test( test4write, "write", buf );
  48. test( test4read, "read", buf );
  49. test( test4write, "write", buf );
  50. test( test4read, "read", buf );
  51. delete[] buf;
  52. return 0;
  53. };
Runtime error #stdin #stdout #stderr 0s 4480KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc