B1KMusic

C Struct / C++ Class comparison

Sep 30th, 2015
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. Let's keep things neat. Rather than post everything in one place, I've pasted each file in its own paste. As a bonus, they also get syntax highlighting.
  2.  
  3. class.cpp:
  4. http://pastebin.com/mUjbcsaL
  5.  
  6. class.h:
  7. http://pastebin.com/0ZK25wvt
  8.  
  9. main.cpp:
  10. http://pastebin.com/hQLYfQEb
  11.  
  12. struct.c:
  13. http://pastebin.com/vyvTm8dS
  14.  
  15. struct.h:
  16. http://pastebin.com/qqjp62QM
  17.  
  18. main.c:
  19. http://pastebin.com/mLS0NVVE
  20.  
  21. The above six files do exactly the same thing, except one is implemented in C as a struct + similarly named functions, while the other is implemented in C++ as a class
  22.  
  23. Edit: I've moved the URLs to their own line so that it's easy to triple-click and select them
  24.  
  25. Here's the output:
  26.  
  27. from main.c + struct.c:
  28.  
  29. % gcc main.c struct.c -o struct
  30. % valgrind ./struct
  31. ==3195== Memcheck, a memory error detector
  32. ==3195== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  33. ==3195== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
  34. ==3195== Command: ./struct
  35. ==3195==
  36. John Smith, a male, is 44 years old
  37. Sally Smythe, a female, is 33 years old
  38. Alice Kingsley, a female, is 86 years old
  39. John Smith says Goodbye
  40. Sally Smythe says Goodbye
  41. ==3195==
  42. ==3195== HEAP SUMMARY:
  43. ==3195== in use at exit: 0 bytes in 0 blocks
  44. ==3195== total heap usage: 2 allocs, 2 frees, 80 bytes allocated
  45. ==3195==
  46. ==3195== All heap blocks were freed -- no leaks are possible
  47. ==3195==
  48. ==3195== For counts of detected and suppressed errors, rerun with: -v
  49. ==3195== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  50.  
  51. Good!
  52. - - - -
  53.  
  54. from main.cpp + class.cpp:
  55.  
  56. % g++ main.cpp class.cpp -o class
  57. main.cpp: In function ‘int main()’:
  58. main.cpp:4:53: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  59. Person *john = new Person("John Smith", 44, male);
  60. ^
  61. main.cpp:5:58: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  62. Person *sally = new Person("Sally Smythe", 33, female);
  63. ^
  64. main.cpp:9:46: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  65. Person local("Alice Kingsley", 86, female);
  66. ^
  67. [Simple warnings about sending string literals. Can be fixed by changing the `char *name` member of Person to `const char *name`.]
  68.  
  69. % valgrind ./class
  70. ==15787== Memcheck, a memory error detector
  71. ==15787== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  72. ==15787== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
  73. ==15787== Command: ./class
  74. ==15787==
  75. John Smith, a male, is 44 years old
  76. Sally Smythe, a female, is 33 years old
  77. Alice Kingsley, a female, is 86 years old
  78. John Smith says Goodbye
  79. Sally Smythe says Goodbye
  80. Alice Kingsley says Goodbye <-- Notice how Alice was destroyed without having to do anything
  81. ==15787==
  82. ==15787== HEAP SUMMARY:
  83. ==15787== in use at exit: 0 bytes in 0 blocks
  84. ==15787== total heap usage: 2 allocs, 2 frees, 32 bytes allocated
  85. ==15787==
  86. ==15787== All heap blocks were freed -- no leaks are possible
  87. ==15787==
  88. ==15787== For counts of detected and suppressed errors, rerun with: -v
  89. ==15787== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  90.  
  91. Good!
  92. - - - -
  93.  
  94. Difference in performance:
  95.  
  96. % time ./struct (5 times)
  97.  
  98. real 0m0.003s 0m0.004s 0m0.003s 0m0.003s 0m0.004s
  99. user 0m0.000s 0m0.000s 0m0.004s 0m0.000s 0m0.000s
  100. sys 0m0.000s 0m0.000s 0m0.000s 0m0.000s 0m0.000s
  101.  
  102. % time ./class (5 times)
  103. real 0m0.005s 0m0.005s 0m0.005s 0m0.004s 0m0.005s
  104. user 0m0.000s 0m0.000s 0m0.000s 0m0.000s 0m0.000s
  105. sys 0m0.000s 0m0.004s 0m0.004s 0m0.000s 0m0.004s
  106.  
  107. Barely a 1/500 second difference
  108.  
  109. - - - -
  110.  
  111. Difference in size (after stripping):
  112.  
  113. % ls -l struct class
  114. -rwxrwxr-x 1 braden braden 6312 Sep 30 08:49 struct
  115. -rwxrwxr-x 1 braden braden 10536 Sep 30 08:49 class
  116.  
  117. The C++ binary Nearly twice the size as the C binary. It's just 4.2 KB though.
  118.  
  119. Since C++ and C work nearly the same, internally, performance is clearly not an issue, however, as you can see, the executable is considerably larger. I wouldn't worry about it, though. This example is small scale, and the difference in size is a mere 4 KB. This more than likely plateaus when it comes to larger projects.
Advertisement
Add Comment
Please, Sign In to add comment