Guest User

Untitled

a guest
Aug 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. Is it more efficient to declare variables late?
  2. int x;
  3. code
  4. ..
  5. .
  6. .
  7. . x is able to be used in all this code
  8. .
  9. actually used here
  10. .
  11. end
  12.  
  13. code
  14. ..
  15. .
  16. .
  17. .
  18. int x;
  19. actually used here
  20. .
  21. end
  22.  
  23. #include <iostream>
  24.  
  25. int main() {
  26. for (int j = 0; j < 1000; ++j) {
  27. std::cout << j << std::endl;
  28. }
  29. int i = 999;
  30. std::cout << i << std::endl;
  31. }
  32.  
  33. #include <iostream>
  34.  
  35. int main() {
  36. int i = 999;
  37. for (int j = 0; j < 1000; ++j) {
  38. std::cout << j << std::endl;
  39. }
  40. std::cout << i << std::endl;
  41. }
  42.  
  43. g++ -Wall -Wextra -O4 -S measure.c
  44. g++ -Wall -Wextra -O4 -S measure2.c
  45.  
  46. < .file "measure2.cc"
  47. ---
  48. > .file "measure.cc"
  49.  
  50. #include <iostream>
  51.  
  52. namespace {
  53. struct foo {
  54. foo() { }
  55. ~foo() { }
  56. };
  57. }
  58.  
  59. std::ostream& operator<<(std::ostream& out, const foo&) {
  60. return out << "foo";
  61. }
  62.  
  63. int main() {
  64. for (int j = 0; j < 1000; ++j) {
  65. std::cout << j << std::endl;
  66. }
  67. foo i;
  68. std::cout << i << std::endl;
  69. }
  70.  
  71. #include <iostream>
  72.  
  73. namespace {
  74. struct foo {
  75. foo() { }
  76. ~foo() { }
  77. };
  78. }
  79.  
  80. std::ostream& operator<<(std::ostream& out, const foo&) {
  81. return out << "foo";
  82. }
  83.  
  84. int main() {
  85. foo i;
  86. for (int j = 0; j < 1000; ++j) {
  87. std::cout << j << std::endl;
  88. }
  89. std::cout << i << std::endl;
  90. }
Add Comment
Please, Sign In to add comment