language: C++11 (gcc-4.7.2)
date: 360 days 8 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <ctime>
#include <chrono>
#include <ostream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
 
class StopWatch
{
public:
        struct Process
        {
                Process(std::string name, double time)
                        :process_name(std::move(name)),
                        running_time(time)
                {}
                
                std::string process_name;
                double running_time;
        };
        
        StopWatch()
                :running_(false)
        {}
 
        void Start(const std::string & process)
        {
                if (running_)
                        Stop();
                current_process_ = process;
                running_ = true;
                start_time_ = std::chrono::system_clock::now();
        }
        
        void Stop()
        {
                if (!running_)
                        return;
                auto now = std::chrono::system_clock::now();
                std::chrono::duration<double> duration = now - start_time_;
                processes_.emplace_back(
                        std::move(current_process_), duration.count());
                running_ = false;
        }
        
        static double SystemClockSeconds()
        {
                
                return (double)clock() / CLOCKS_PER_SEC;
        }
        
        double operator[] (const std::string & process) const
        {
                auto it = std::find_if(
                        processes_.begin(), processes_.end(),
                        [&](const Process & p) {
                                return p.process_name == process;
                        });
                        
                if (it != processes_.end())
                        return it->running_time;
                return 0.0;
        }
        
        Process operator[](int index) const {
                return processes_[index];
        }
        
        int size() const { return processes_.size(); }
private:
        bool running_;
        std::string current_process_;
        std::chrono::system_clock::time_point start_time_;
        std::vector<Process> processes_;                
};
 
std::ostream & operator<<(
        std::ostream & os, const StopWatch & watch)
{
        for (int i=0; i<watch.size(); ++i)
                os << watch[i].process_name << " : "
                   << watch[i].running_time << "s\n";
        return os;
}
 
int main()
{
        const int sz = 1000000;
        std::vector<long long> src;
        std::vector<long long> vi;
        std::vector<long long*> vp;
        
        for (int i=0; i<sz; ++i)
        {
                int x = rand();
                src.push_back(x);
                vi.push_back(x);
        }
        
        for (int i=0; i<sz; ++i)
        {
                vp.push_back(&src[i]);
        }
        
        StopWatch watch;
        watch.Start("integers");
        std::sort(vi.begin(), vi.end(),
            [](long long lhs, long long rhs) {
                        return lhs < rhs;
                });
        watch.Stop();
        
        watch.Start("pointers");
        std::sort(vp.begin(), vp.end(),
            [](long long * lhs, long long * rhs) {
                        return *lhs < *rhs;
                });
        watch.Stop();
        
        std::cout << watch;
}