fork(9) download
  1. #include <iostream>
  2. #include <chrono>
  3. using namespace std;
  4.  
  5. typedef std::chrono::high_resolution_clock Clock;
  6.  
  7.  
  8. class Dimension
  9. {
  10. public:
  11. Dimension(int _X, int _Y)
  12. {
  13. mX = _X;
  14. mY = _Y;
  15. }
  16.  
  17. private:
  18. int mX;
  19. int mY;
  20. };
  21.  
  22. template <class T>
  23. class Image
  24. {
  25. public:
  26. void Draw()
  27. {
  28. // Dispatch call to exact type
  29. static_cast<T*>(this)->Draw();
  30. }
  31.  
  32. Dimension GetDimensionInPixels()
  33. {
  34. // Dispatch call to exact type
  35. static_cast<T*>(this)->GetDimensionInPixels();
  36. }
  37.  
  38. protected:
  39. int dimensionX;
  40. int dimensionY;
  41. };
  42.  
  43. class PngImage : public Image<PngImage>
  44. {
  45. public:
  46. void Draw()
  47. {
  48. }
  49.  
  50. Dimension GetDimensionInPixels()
  51. {
  52. return Dimension(dimensionX, dimensionY);
  53. }
  54. };
  55.  
  56. class BitmapImage : public Image<BitmapImage>
  57. {
  58. public:
  59. void Draw()
  60. {
  61. }
  62.  
  63. Dimension GetDimensionInPixels()
  64. {
  65. return Dimension(dimensionX, dimensionY);
  66. }
  67. };
  68.  
  69. class TiffImage : public Image<TiffImage>
  70. {
  71. public:
  72. void Draw()
  73. {
  74. // Uncomment this to check method dispatch
  75. // cout << "TiffImage::Draw() called" << endl;
  76. }
  77.  
  78. Dimension GetDimensionInPixels()
  79. {
  80. return Dimension(dimensionX, dimensionY);
  81. }
  82. };
  83.  
  84. int main() {
  85.  
  86. Image<TiffImage>* pImage = new TiffImage;
  87.  
  88. auto then = Clock::now();
  89.  
  90. // Call Draw 1000 times to make sure perf is visible
  91. for (int i = 0; i < 1000; ++i)
  92. pImage->Draw();
  93.  
  94. auto now = Clock::now();
  95.  
  96. cout << "Time taken: "
  97. << std::chrono::duration_cast<std::chrono::nanoseconds>(now - then).count()
  98. << " nanoseconds" << std::endl;
  99.  
  100. return 0;
  101. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Time taken: 732 nanoseconds