-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumba_cpp.cpp
executable file
·197 lines (158 loc) · 4.15 KB
/
numba_cpp.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* A program that checks the performance
* of a single processor core by finding
* all prime numbers (0-199999 by default),
* measuring the time needed by the
* platform and writing the result to the log
*
* Autor: Cixo
*/
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <chrono>
#include <cstdlib>
/* Defaults options */
#define DEFAULT_BENCHMARK_SIZE 200000
#define DEFAULT_LOG_FILE "numba_cpp.log"
/* Help text for -h option */
#define HELP_TEXT \
"Hello in NumbaBenchmark!\n" \
"This benchmark is used to test a single CPU " \
"core by looking for primes in a given range. " \
"The program measures how much time is needed " \
"to perform the calculations and saves the " \
"result to a file.\n Options:\n\t-s : tested " \
"interval (default: " << DEFAULT_BENCHMARK_SIZE \
<< ")\n\t-f : log file name (default: " \
<< DEFAULT_LOG_FILE << ")\n\t-h : This help " \
"note\n"
/*
* This function checks if the given
* number is prime or not prime. If
* prime then return true, else
* return false.
*
* Params:
* ++ number (int): Number to check
*
* Return:
* ++ true: Number is prime
* ++ false: Number is not prime
*/
static bool
isPrime (int number)
{
/* Get absolute of number */
number = abs (number);
/* Zero and one is not prime */
if (number == 0 || number == 1)
return false;
/* Check all dividers */
for (int test = 2; test < number; test ++)
if (number % test == 0)
return false;
return true;
}
/*
* This function creates a benchmark
* summary from elapsed time and
* benchmark range size.
*
* Params:
* ++ time (long): Elapsed time in us
* ++ side (int): Benchmark area size
*
* Return:
* ++ string: Benchmark summary
*/
static std::string
createSummary (long time, int size)
{
std::string summary;
summary += "Benchmark results:\n";
summary += "\tTime elapsed: " + std::to_string ((double) (time) / 1000000) + "\n";
summary += "\tBenchmark size: 0 - " + std::to_string (size) + "\n";
return summary;
}
/*
* This function creates a single
* number summary to add it to log.
*
* Params:
* ++ number (int): Number for summary
*
* Return:
* ++ string: Number summary
*/
static std::string
createNumberSummary (int number)
{
std::string summary;
summary += std::to_string (number);
summary += ": ";
summary += (isPrime (number)) ? "prime" : "not prime";
summary += "\n";
return summary;
}
int
main (int argc, char *argv[])
{
/* Set default options */
int benchmark_size = DEFAULT_BENCHMARK_SIZE;
std::string log_file_name = DEFAULT_LOG_FILE;
/* Parsing command line options */
for (int count = 0; count < argc; count ++) {
/* To change range size */
if ((std::string) (argv[count]) == "-s") {
if (count + 1 < argc)
benchmark_size = std::stoi (argv[++ count]);
}
/* To change log file */
if ((std::string) (argv[count]) == "-f") {
if (count + 1 < argc)
log_file_name = argv[++ count];
}
/* To show help note */
if ((std::string) (argv[count]) == "-h") {
std::cout << HELP_TEXT;
return 0;
}
}
/* Create variable to save log */
std::string log;
/* Save start time */
auto start = std::chrono::steady_clock::now ();
/* Check all numbers in range */
for (int count = 0; count < benchmark_size; count ++)
log += createNumberSummary (count);
/* Save end time */
auto end = std::chrono::steady_clock::now ();
/* Calculate elapsed time */
long time_elapsed = (long)
(std::chrono::duration_cast <std::chrono::microseconds> (end - start).count ());
/* Create benchmark summary */
std::string summary = createSummary (time_elapsed, benchmark_size);
/* Display benchmark summary */
std::cout << summary;
/* Save benchmark summary to log */
log += "\n\n";
log += summary;
/* Variable to open log file */
std::fstream log_file;
/* Open log file */
log_file.open (log_file_name, std::ios::out | std::ios::trunc);
/* If file is not open propertly */
if (!log_file.good ()){
/* Display log on screen */
std::cout << "\n\n";
std::cout << "Save to log error!\n";
std::cout << "Log:\n" << log;
return 1;
}
/* Saving log file */
log_file << log;
log_file.close ();
return 0;
}