File: store_now_in_vector.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (154 lines) | stat: -rw-r--r-- 3,385 bytes parent folder | download | duplicates (3)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//  Copyright 2011 Vicente J. Botet Escriba
//  Copyright (c) Microsoft Corporation 2014

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

#include "../example/timer.hpp"
#include <boost/chrono/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>
#include <boost/chrono/process_cpu_clocks.hpp>
#include <vector>
#include <iostream>

//#define BOOST_CHRONO_HAS_TIMES_AND_CLOCK

#ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK
#include <sys/time.h> //for gettimeofday and timeval
#include <sys/times.h> //for times
#include <unistd.h>
#endif


static const std::size_t size = 1000000;

typedef boost_ex::chrono::timer<boost::chrono::high_resolution_clock> Stopwatch;

template <typename Clock>
void perf_constant(std::vector<typename Clock::time_point>& vec)
{
  for (int i=size-1; i>=0; --i)
  {
    vec[i]=typename Clock::time_point();
  }
}

template <typename Clock>
void perf(std::vector<typename Clock::time_point>& vec)
{
  for (int i=size-1; i>=0; --i)
  {
    vec[i]=Clock::now();
  }
}

template <typename Clock>
void test()
{
  std::vector<typename Clock::time_point> vec(size);
  Stopwatch sw1;
  perf_constant<Clock>(vec);
  Stopwatch::duration t1 = sw1.elapsed();
  Stopwatch sw2;
  perf<Clock>(vec);
  Stopwatch::duration t2 = sw2.elapsed();
  std::cout <<" "<< (t2-t1) << std::endl;
  //std::cout <<" "<< ((t2-t1)/size) << std::endl;
  std::size_t cnt=0;
  for (int i=size-1; i>0; --i)
  {
    if (vec[i]!=vec[i-1]) ++cnt;
  }
  std::cout <<"changes: "<< cnt << std::endl;

}





#ifdef BOOST_CHRONO_HAS_TIMES_AND_CLOCK
void perf2(std::vector<clock_t>& vec)
{
  Stopwatch sw;
  for (int i=size-1; i>=0; --i)
  {
    tms tm;
    vec[i]=::times(&tm);
  }
  std::cout << sw.elapsed() << std::endl;
}

void perf3(std::vector<clock_t>& vec)
{
  Stopwatch sw;
  for (int i=size-1; i>=0; --i)
  {
    vec[i]=::clock();
  }
  std::cout << sw.elapsed() << std::endl;
}

void test2()
{
  std::vector<clock_t> vec(size);
  perf2(vec);
  std::size_t cnt=0;
  for (int i=10; i>0; --i)
  {
    if (vec[i]!=vec[i-1]) ++cnt;
    std::cout << vec[i] << " " ;
  }
  std::cout<< std::endl;
  std::cout <<"changes: "<< cnt << std::endl;
}

void test3()
{
  std::vector<clock_t> vec(size);
  perf3(vec);
  std::size_t cnt=0;
  for (int i=10; i>0; --i)
  {
    if (vec[i]!=vec[i-1]) ++cnt;
    std::cout << vec[i] << " " ;
  }
  std::cout<< std::endl;
  std::cout <<"changes: "<< cnt << std::endl;
}

#endif

int main() {

  std::cout << "system_clock ";
  test<boost::chrono::system_clock>();
#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  std::cout << "steady_clock " ;
  test<boost::chrono::steady_clock>();
#endif
  std::cout << "high_resolution_clock " ;
  test<boost::chrono::high_resolution_clock>();

#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
  std::cout << "process_real_cpu_clock ";
  test<boost::chrono::process_real_cpu_clock>();
#if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
  std::cout << "process_user_cpu_clock ";
  test<boost::chrono::process_user_cpu_clock>();
  std::cout << "process_system_cpu_clock " ;
  test<boost::chrono::process_system_cpu_clock>();
  std::cout << "process_cpu_clock " ;
  test<boost::chrono::process_cpu_clock>();
#endif
#endif
  std::cout << "system_clock ";
  test<boost::chrono::system_clock>();
#if 0
  std::cout << "times ";
  test2();
  std::cout << "clock ";
  test3();
#endif
  return 0;
}