-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtime_parallel_for_each.cpp
More file actions
70 lines (52 loc) · 2.07 KB
/
time_parallel_for_each.cpp
File metadata and controls
70 lines (52 loc) · 2.07 KB
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
/*
Copyright (c) 2005-2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <vector>
#include <list>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include "tbb/parallel_for_each.h"
#include "tbb/tick_count.h"
template <typename Type>
void foo( Type &f ) {
f += 1.0f;
}
template <typename Container>
void test( std::string testName, const int N, const int numRepeats ) {
typedef typename Container::value_type Type;
Container v;
for ( int i = 0; i < N; ++i ) {
v.push_back( static_cast<Type>(std::rand()) );
}
std::vector<double> times;
times.reserve( numRepeats );
for ( int i = 0; i < numRepeats; ++i ) {
tbb::tick_count t0 = tbb::tick_count::now();
tbb::parallel_for_each( v.begin(), v.end(), foo<Type> );
tbb::tick_count t1 = tbb::tick_count::now();
times.push_back( (t1 - t0).seconds()*1e+3 );
}
std::sort( times.begin(), times.end() );
std::cout << "Test " << testName << std::endl
<< "min " << times[times.size() / 20] << " ms " << std::endl
<< "med " << times[times.size() / 2] << " ms " << std::endl
<< "max " << times[times.size() - times.size() / 20 - 1] << " ms " << std::endl;
}
int main( int argc, char* argv[] ) {
const int N = argc > 1 ? std::atoi( argv[1] ) : 10 * 1000;
const int numRepeats = argc > 2 ? std::atoi( argv[2] ) : 10;
test< std::vector<float> >( "std::vector<float>", N, numRepeats );
test< std::list<float> >( "std::list<float>", N / 100, numRepeats );
return 0;
}