fork download
  1. #include <cstdio> // for printf.
  2. #include <chrono> // for milliseconds, steady_clcok, duration_cast.
  3.  
  4. const int N = 10 * 1000;
  5.  
  6. struct point
  7. {
  8. point(double x, double y)
  9. : x(x), y(y)
  10. {
  11. }
  12.  
  13. double x;
  14. double y;
  15. };
  16.  
  17. struct line
  18. {
  19. line(double x1, double y1, double x2, double y2)
  20. : p_p1(new point(x1, y1)), p_p2(new point(x2, y2))
  21. {
  22. }
  23.  
  24. ~line() throw()
  25. {
  26. delete this->p_p2;
  27. delete this->p_p1;
  28. }
  29.  
  30. point *p_p1;
  31. point *p_p2;
  32. };
  33.  
  34. void f()
  35. {
  36. auto * * array1 = new line *[N];
  37.  
  38. auto st = std::chrono::steady_clock::now();
  39. for ( auto i = 0; i < N; i++ )
  40. array1[i] = new line(1, 2, 3, 4);
  41. for ( auto i = 0; i < N; i++ )
  42. delete array1[i];
  43. auto et = std::chrono::steady_clock::now();
  44. delete array1;
  45.  
  46. std::printf("total = %lld ms\n", std::chrono::duration_cast<std::chrono::milliseconds>(et - st).count());
  47. std::printf("avg. = %.1f us\n", std::chrono::duration_cast<std::chrono::milliseconds>(et - st).count() / static_cast<double>(N) * 1000);
  48. }
  49.  
  50. int main()
  51. {
  52. for ( auto i = 0; i < 3; i++ )
  53. f();
  54.  
  55. getchar();
  56. }
  57.  
  58. /*
  59. C#はこれ
  60.  
  61. using System;
  62.  
  63. static class Test
  64. {
  65. const int N = 100 * 1000;
  66.  
  67. class point
  68. {
  69. public point() { }
  70.  
  71. public point(double x, double y)
  72. {
  73. this.x = x;
  74. this.y = y;
  75. }
  76.  
  77. public double x;
  78. public double y;
  79. };
  80.  
  81. class line
  82. {
  83. public line(double x1, double y1, double x2, double y2)
  84. {
  85. this.p_p1.x = x1;
  86. this.p_p1.y = y1;
  87. this.p_p2.x = x2;
  88. this.p_p2.y = y2;
  89. }
  90.  
  91. public point p_p1 = new point();
  92. public point p_p2 = new point();
  93. };
  94.  
  95. static void f()
  96. {
  97. var array1 = new line[N];
  98.  
  99. var st = DateTime.Now;
  100. for ( var i = 0; i < N; i++ )
  101. array1[i] = new line(1, 2, 3, 4);
  102. array1 = null;
  103. System.GC.Collect(3, GCCollectionMode.Default, true);
  104. var et = DateTime.Now;
  105.  
  106. Console.WriteLine("total = {0:0.0} ms", (et - st).TotalMilliseconds);
  107. Console.WriteLine("avg. = {0:0.000} us", (et - st).TotalMilliseconds / (double)N * 1000);
  108. }
  109.  
  110. static void Main()
  111. {
  112. for ( var i = 0; i < 10; i++ )
  113. f();
  114.  
  115. Console.ReadKey();
  116. }
  117.  
  118. }
  119.  
  120. */
  121.  
Success #stdin #stdout 0.01s 3552KB
stdin
Standard input is empty
stdout
total = 3 ms
avg. = 0.3 us
total = 2 ms
avg. = 0.2 us
total = 2 ms
avg. = 0.2 us