Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- class.cpp:
- http://pastebin.com/mUjbcsaL
- class.h:
- http://pastebin.com/0ZK25wvt
- main.cpp:
- http://pastebin.com/hQLYfQEb
- struct.c:
- http://pastebin.com/vyvTm8dS
- struct.h:
- http://pastebin.com/qqjp62QM
- main.c:
- http://pastebin.com/mLS0NVVE
- 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
- Edit: I've moved the URLs to their own line so that it's easy to triple-click and select them
- Here's the output:
- from main.c + struct.c:
- % gcc main.c struct.c -o struct
- % valgrind ./struct
- ==3195== Memcheck, a memory error detector
- ==3195== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
- ==3195== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
- ==3195== Command: ./struct
- ==3195==
- John Smith, a male, is 44 years old
- Sally Smythe, a female, is 33 years old
- Alice Kingsley, a female, is 86 years old
- John Smith says Goodbye
- Sally Smythe says Goodbye
- ==3195==
- ==3195== HEAP SUMMARY:
- ==3195== in use at exit: 0 bytes in 0 blocks
- ==3195== total heap usage: 2 allocs, 2 frees, 80 bytes allocated
- ==3195==
- ==3195== All heap blocks were freed -- no leaks are possible
- ==3195==
- ==3195== For counts of detected and suppressed errors, rerun with: -v
- ==3195== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
- Good!
- - - - -
- from main.cpp + class.cpp:
- % g++ main.cpp class.cpp -o class
- main.cpp: In function ‘int main()’:
- main.cpp:4:53: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
- Person *john = new Person("John Smith", 44, male);
- ^
- main.cpp:5:58: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
- Person *sally = new Person("Sally Smythe", 33, female);
- ^
- main.cpp:9:46: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
- Person local("Alice Kingsley", 86, female);
- ^
- [Simple warnings about sending string literals. Can be fixed by changing the `char *name` member of Person to `const char *name`.]
- % valgrind ./class
- ==15787== Memcheck, a memory error detector
- ==15787== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
- ==15787== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
- ==15787== Command: ./class
- ==15787==
- John Smith, a male, is 44 years old
- Sally Smythe, a female, is 33 years old
- Alice Kingsley, a female, is 86 years old
- John Smith says Goodbye
- Sally Smythe says Goodbye
- Alice Kingsley says Goodbye <-- Notice how Alice was destroyed without having to do anything
- ==15787==
- ==15787== HEAP SUMMARY:
- ==15787== in use at exit: 0 bytes in 0 blocks
- ==15787== total heap usage: 2 allocs, 2 frees, 32 bytes allocated
- ==15787==
- ==15787== All heap blocks were freed -- no leaks are possible
- ==15787==
- ==15787== For counts of detected and suppressed errors, rerun with: -v
- ==15787== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
- Good!
- - - - -
- Difference in performance:
- % time ./struct (5 times)
- real 0m0.003s 0m0.004s 0m0.003s 0m0.003s 0m0.004s
- user 0m0.000s 0m0.000s 0m0.004s 0m0.000s 0m0.000s
- sys 0m0.000s 0m0.000s 0m0.000s 0m0.000s 0m0.000s
- % time ./class (5 times)
- real 0m0.005s 0m0.005s 0m0.005s 0m0.004s 0m0.005s
- user 0m0.000s 0m0.000s 0m0.000s 0m0.000s 0m0.000s
- sys 0m0.000s 0m0.004s 0m0.004s 0m0.000s 0m0.004s
- Barely a 1/500 second difference
- - - - -
- Difference in size (after stripping):
- % ls -l struct class
- -rwxrwxr-x 1 braden braden 6312 Sep 30 08:49 struct
- -rwxrwxr-x 1 braden braden 10536 Sep 30 08:49 class
- The C++ binary Nearly twice the size as the C binary. It's just 4.2 KB though.
- 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