forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.cpp.html
119 lines (107 loc) · 15 KB
/
counter.cpp.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>counter.cpp</title>
</head>
<body style="background-color:white">
<pre><b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><time.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><sys/time.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><sstream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdlib.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdio.h></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// this is the timer class from lab 6, included here so as not to</span></i>
<i><span style="color:#9A1900">// require multiple files to compile</span></i>
<b><span style="color:#0000FF">class</span></b> <span style="color:#008080">timer</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">private</span></b><span style="color:#990000">:</span>
<span style="color:#008080">timeval</span> startVar<span style="color:#990000">,</span> stopVar<span style="color:#990000">;</span>
<span style="color:#009900">bool</span> running<span style="color:#990000">;</span>
<b><span style="color:#0000FF">public</span></b><span style="color:#990000">:</span>
<b><span style="color:#000000">timer</span></b><span style="color:#990000">()</span> <span style="color:#990000">:</span> <b><span style="color:#000000">running</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#FF0000">{}</span>
<span style="color:#990000">~</span><b><span style="color:#000000">timer</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{}</span>
<b><span style="color:#000000">timer</span></b><span style="color:#990000">(</span>timer <span style="color:#990000">&</span> myTimer<span style="color:#990000">);</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">start</span></b><span style="color:#990000">();</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">stop</span></b><span style="color:#990000">();</span>
<span style="color:#008080">string</span> <b><span style="color:#000000">toString</span></b><span style="color:#990000">();</span>
ostream <span style="color:#990000">&</span> <b><span style="color:#000000">print</span></b><span style="color:#990000">(</span><span style="color:#008080">ostream</span> <span style="color:#990000">&</span>theStream<span style="color:#990000">);</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
timer<span style="color:#990000">::</span><b><span style="color:#000000">timer</span></b><span style="color:#990000">(</span>timer <span style="color:#990000">&</span> t<span style="color:#990000">)</span> <span style="color:#990000">:</span> <b><span style="color:#000000">running</span></b><span style="color:#990000">(</span>t<span style="color:#990000">.</span>running<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">memcpy</span></b><span style="color:#990000">(&</span>startVar<span style="color:#990000">,</span> <span style="color:#990000">&(</span>t<span style="color:#990000">.</span>startVar<span style="color:#990000">),</span> <b><span style="color:#0000FF">sizeof</span></b> <span style="color:#990000">(</span>timeval<span style="color:#990000">));</span>
<b><span style="color:#000000">memcpy</span></b><span style="color:#990000">(&</span>stopVar<span style="color:#990000">,</span> <span style="color:#990000">&(</span>t<span style="color:#990000">.</span>stopVar<span style="color:#990000">),</span> <b><span style="color:#0000FF">sizeof</span></b> <span style="color:#990000">(</span>timeval<span style="color:#990000">));</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> timer<span style="color:#990000">::</span><b><span style="color:#000000">start</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(!</span>running<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
running <span style="color:#990000">=</span> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<b><span style="color:#000000">gettimeofday</span></b><span style="color:#990000">(&</span>startVar<span style="color:#990000">,</span>NULL<span style="color:#990000">);</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">int</span> timer<span style="color:#990000">::</span><b><span style="color:#000000">stop</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>running<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
running <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<b><span style="color:#000000">gettimeofday</span></b><span style="color:#990000">(&</span>stopVar<span style="color:#990000">,</span>NULL<span style="color:#990000">);</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
ostream <span style="color:#990000">&</span> timer<span style="color:#990000">::</span><b><span style="color:#000000">print</span></b><span style="color:#990000">(</span>ostream <span style="color:#990000">&</span> out<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#990000">(</span>out <span style="color:#990000"><<</span> <b><span style="color:#000000">toString</span></b><span style="color:#990000">());</span>
<span style="color:#FF0000">}</span>
<span style="color:#008080">string</span> timer<span style="color:#990000">::</span><b><span style="color:#000000">toString</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">ostringstream</span> out<span style="color:#990000">;</span>
<span style="color:#009900">int</span> ms<span style="color:#990000">,</span> s<span style="color:#990000">,</span> m<span style="color:#990000">,</span> h<span style="color:#990000">;</span>
<span style="color:#009900">int</span> totalmsec<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>running<span style="color:#990000">)</span>
out <span style="color:#990000"><<</span> <span style="color:#FF0000">"Timer still running</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">"</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">time_t</span> sec <span style="color:#990000">=</span> stopVar<span style="color:#990000">.</span>tv_sec <span style="color:#990000">-</span> startVar<span style="color:#990000">.</span>tv_sec<span style="color:#990000">;</span>
<span style="color:#009900">long</span> usec <span style="color:#990000">=</span> stopVar<span style="color:#990000">.</span>tv_usec <span style="color:#990000">-</span> startVar<span style="color:#990000">.</span>tv_usec<span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> usec <span style="color:#990000"><</span> <span style="color:#993399">0</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
sec<span style="color:#990000">--;</span>
usec <span style="color:#990000">+=</span> <span style="color:#993399">1000000</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
out <span style="color:#990000"><<</span> <span style="color:#990000">(</span>sec<span style="color:#990000">*</span><span style="color:#993399">1000</span><span style="color:#990000">+</span>usec<span style="color:#990000">/</span><span style="color:#993399">1000</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> out<span style="color:#990000">.</span><b><span style="color:#000000">str</span></b><span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
ostream <span style="color:#990000">&</span> <b><span style="color:#0000FF">operator</span></b><span style="color:#990000"><<(</span><span style="color:#008080">ostream</span> <span style="color:#990000">&</span>out<span style="color:#990000">,</span> <span style="color:#008080">timer</span> <span style="color:#990000">&</span>t<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> t<span style="color:#990000">.</span><b><span style="color:#000000">print</span></b><span style="color:#990000">(</span>out<span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// this main method just counts the time taken to iterate 1 billion</span></i>
<i><span style="color:#9A1900">// times through an idle loop</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span> <span style="color:#990000">**</span>argv<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// check the number of parameters</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> argc <span style="color:#990000">!=</span> <span style="color:#993399">2</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"usage: "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">" <iterations>"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// convert the second parameter to a int</span></i>
<span style="color:#009900">int</span> e<span style="color:#990000">,</span> ret<span style="color:#990000">;</span>
ret <span style="color:#990000">=</span> <b><span style="color:#000000">sscanf</span></b> <span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">],</span> <span style="color:#FF0000">"%d"</span><span style="color:#990000">,</span> <span style="color:#990000">&</span>e<span style="color:#990000">);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> ret <span style="color:#990000">!=</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cerr <span style="color:#990000"><<</span> <span style="color:#FF0000">"error reading input: '"</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">"'"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b> <span style="color:#990000">(</span><span style="color:#993399">2</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// compute 10^e</span></i>
<span style="color:#009900">long</span> n <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> e<span style="color:#990000">;</span> i<span style="color:#990000">++</span> <span style="color:#990000">)</span>
n <span style="color:#990000">*=</span> <span style="color:#993399">10</span><span style="color:#990000">;</span>
<span style="color:#008080">timer</span> t<span style="color:#990000">;</span>
t<span style="color:#990000">.</span><b><span style="color:#000000">start</span></b><span style="color:#990000">();</span>
<i><span style="color:#9A1900">// do something that takes some time...</span></i>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">long</span> i <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> n<span style="color:#990000">;</span> i<span style="color:#990000">++</span> <span style="color:#990000">);</span>
t<span style="color:#990000">.</span><b><span style="color:#000000">stop</span></b><span style="color:#990000">();</span>
cout <span style="color:#990000"><<</span> t <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>