Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/time.h>
- #include <string>
- #include <atomic>
- #include <functional>
- size_t now() {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_sec * 1000000 + tv.tv_usec;
- }
- class Long {
- public:
- void CtorAndDtor() {
- long a = 2334543523423423;
- }
- void Copy() {
- long a = 234235345435345;
- long b = a;
- }
- void Compare() {
- long a = 234454353453452;
- long b = 489123710234823;
- if (a == b) {
- return;
- }
- }
- size_t Hash() {
- long a = 234234324324324;
- return a;
- }
- };
- // all string is 16 length
- // it will not trigger malloc in +gcc5.1
- class String {
- public:
- void CtorAndDtor() {
- std::string a = "1234567890123456";
- }
- void Copy() {
- std::string a = "1234567890123456";
- std::string b = a;
- }
- void Compare() {
- std::string a = "1234567890123456";
- std::string b = "8973478163409324";
- if (a == b) {
- return;
- }
- }
- size_t Hash() {
- std::string a = "1234567890123456";
- return std::hash<std::string>{}(a);
- }
- };
- #define BENCH_MARK(TYPE, FUNC, LOOP_NUM, COST) do { \
- TYPE object; \
- auto begin = now(); \
- std::atomic_thread_fence(std::memory_order_acq_rel); \
- for (size_t i = 0; i < LOOP_NUM; i++) { \
- object.FUNC(); \
- } \
- std::atomic_thread_fence(std::memory_order_acq_rel); \
- auto end = now(); \
- COST = end - begin; \
- } while (0)
- int main(int argc, char** argv) {
- size_t loop_num = atoi(argv[1]);
- size_t Long_CtorAndDtor_Cost = 0;
- size_t String_CtorAndDtor_Cost = 0;
- BENCH_MARK(Long, CtorAndDtor, loop_num, Long_CtorAndDtor_Cost);
- BENCH_MARK(String, CtorAndDtor, loop_num, String_CtorAndDtor_Cost);
- size_t Long_Copy_Cost = 0;
- size_t String_Copy_Cost = 0;
- BENCH_MARK(Long, Copy, loop_num, Long_Copy_Cost);
- BENCH_MARK(String, Copy, loop_num, String_Copy_Cost);
- size_t Long_Compare_Cost = 0;
- size_t String_Compare_Cost = 0;
- BENCH_MARK(Long, Compare, loop_num, Long_Compare_Cost);
- BENCH_MARK(String, Compare, loop_num, String_Compare_Cost);
- size_t Long_Hash_Cost = 0;
- size_t String_Hash_Cost = 0;
- BENCH_MARK(Long, Hash, loop_num, Long_Hash_Cost);
- BENCH_MARK(String, Hash, loop_num, String_Hash_Cost);
- size_t CtorAndDtor_Ratio = Long_CtorAndDtor_Cost == 0 ? 0 : String_CtorAndDtor_Cost / Long_CtorAndDtor_Cost;
- std::cout << "CtorAndDtor Long(" << Long_CtorAndDtor_Cost << "us) -vs- String(" << String_CtorAndDtor_Cost << "us) " << CtorAndDtor_Ratio << std::endl;
- size_t Long_Copy_Real = Long_Copy_Cost - Long_CtorAndDtor_Cost;
- size_t String_Copy_Real = String_Copy_Cost - String_CtorAndDtor_Cost;
- size_t Copy_Ratio = Long_Copy_Real == 0 ? 0 : String_Copy_Real / Long_Copy_Real;
- std::cout << "Copy Long(" << Long_Copy_Real << "us) -vs- String(" << String_Copy_Real << "us) " << Copy_Ratio << std::endl;
- size_t Long_Compare_Real = Long_Compare_Cost - Long_CtorAndDtor_Cost;
- size_t String_Compare_Real = String_Compare_Cost - String_CtorAndDtor_Cost;
- size_t Compare_Ratio = Long_Compare_Real == 0 ? 0 : String_Compare_Real / Long_Compare_Real;
- std::cout << "Compare Long(" << Long_Compare_Real << "us) -vs- String(" << String_Compare_Real << "us) " << Compare_Ratio << std::endl;
- size_t Long_Hash_Real = Long_Hash_Cost - Long_CtorAndDtor_Cost;
- size_t String_Hash_Real = String_Hash_Cost - String_CtorAndDtor_Cost;
- size_t Hash_Ratio = Long_Hash_Real == 0 ? 0 : String_Hash_Real / Long_Hash_Real;
- std::cout << "Hash Long(" << Long_Hash_Real << "us) -vs- String(" << String_Hash_Real << "us) " << Hash_Ratio << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment