Guest User

Untitled

a guest
Jan 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. // Rust version that takes several seconds to execute based on the size of Ent:
  2.  
  3. // Strangely runs fine on Playground but not from the executable generated by rustc on desktop
  4.  
  5. struct Ent
  6. {
  7. x : i32,
  8. foo : [i32; 100000]
  9. }
  10.  
  11. impl Ent
  12. {
  13. fn do_thing(&mut self)
  14. {
  15. self.x += 2;
  16. }
  17. }
  18.  
  19. fn main()
  20. {
  21. let mut list : Vec<Ent> = Vec::new();
  22.  
  23. for _ in 0..100
  24. {
  25. let entity = Ent { x : 0, foo : [0; 100000] };
  26.  
  27. list.push(entity);
  28. }
  29.  
  30. for _ in 0..4000000
  31. {
  32. for entity in &mut list
  33. {
  34. entity.do_thing();
  35. }
  36. }
  37. }
  38.  
  39. // CPP version that executes instantaneously:
  40.  
  41. /*
  42.  
  43. #include <vector>
  44.  
  45. struct Ent
  46. {
  47. int x;
  48. int foo[100000];
  49.  
  50. Ent() : x(0), foo() {}
  51.  
  52. void doThing()
  53. {
  54. x += 2;
  55. }
  56. };
  57.  
  58. int main()
  59. {
  60. std::vector<Ent> list;
  61.  
  62. for (int i = 0; i < 100; ++i)
  63. {
  64. Ent entity;
  65.  
  66. list.push_back(entity);
  67. }
  68.  
  69. for (int i = 0; i < 4000000; ++i)
  70. {
  71. for (auto iter = list.begin(); iter != list.end(); ++iter)
  72. {
  73. iter->doThing();
  74. }
  75. }
  76.  
  77. return 0;
  78. }
  79.  
  80. */
Add Comment
Please, Sign In to add comment