fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <array>
  5. using namespace std;
  6.  
  7.  
  8. struct float2
  9. {
  10. float x, y ;
  11. };
  12.  
  13. float2 make_float2(float x, float y) {
  14.  
  15. return float2{x, y};
  16.  
  17. }
  18.  
  19. void originalMethod() {
  20. std::vector<std::vector<float>> pts;
  21. for (int val = 0; val < 10000; ++val) {
  22. std::vector<float> p1{float(val),float(val+1)};
  23. pts.push_back(p1);
  24. }
  25.  
  26. auto start = chrono::steady_clock::now();
  27. std::vector<float2> lattice;
  28. lattice.reserve(pts.size());
  29. for (auto p : pts){
  30. lattice.emplace_back(make_float2(p[0],p[1]));
  31. }
  32. auto end = chrono::steady_clock::now();
  33. auto diff = end - start;
  34. cout << chrono::duration <double, nano> (diff).count() << " ns" << endl;
  35. }
  36.  
  37. void newMethod() {
  38. std::vector<std::array<float, 2>> pts;
  39. for (int val = 0; val < 10000; ++val) {
  40. std::array<float, 2> p1{float(val),float(val+1)};
  41. pts.push_back(p1);
  42. }
  43.  
  44. auto start = chrono::steady_clock::now();
  45. std::vector<float2> lattice;
  46. for (auto p : pts){
  47. if (p.size() == 2) {
  48. float2 p_ = make_float2(p[0],p[1]);
  49. lattice.push_back(p_);
  50. }
  51.  
  52. }
  53. auto end = chrono::steady_clock::now();
  54. auto diff = end - start;
  55. cout << chrono::duration <double, nano> (diff).count() << " ns" << endl;
  56. }
  57.  
  58.  
  59. int main() {
  60.  
  61. originalMethod();
  62. newMethod();
  63. // your code goes here
  64. return 0;
  65. }
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
519839 ns
110164 ns