1 /* 2 Copyright (c) 2023 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "fibonacci_single_task.h" 18 #include "fibonacci_two_tasks.h" 19 20 #include <iostream> 21 #include <numeric> 22 #include <utility> 23 24 int cutoff; 25 26 template <typename F> 27 std::pair</* result */ unsigned long, /* time */ unsigned long> measure(F&& f, 28 int number, 29 unsigned long ntrial) { 30 std::vector<unsigned long> times; 31 32 unsigned long result; 33 for (unsigned long i = 0; i < ntrial; ++i) { 34 auto t1 = std::chrono::steady_clock::now(); 35 result = f(number); 36 auto t2 = std::chrono::steady_clock::now(); 37 38 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count(); 39 times.push_back(time); 40 } 41 42 return std::make_pair( 43 result, 44 static_cast<unsigned long>(std::accumulate(times.begin(), times.end(), 0) / times.size())); 45 } 46 47 int main(int argc, char* argv[]) { 48 int numbers = argc > 1 ? strtol(argv[1], nullptr, 0) : 50; 49 cutoff = argc > 2 ? strtol(argv[2], nullptr, 0) : 16; 50 unsigned long ntrial = argc > 3 ? (unsigned long)strtoul(argv[3], nullptr, 0) : 20; 51 52 auto res = measure(fibonacci_two_tasks, numbers, ntrial); 53 std::cout << "Fibonacci two tasks impl N = " << res.first << " Avg time = " << res.second 54 << " ms" << std::endl; 55 56 res = measure(fibonacci_single_task, numbers, ntrial); 57 std::cout << "Fibonacci single task impl N = " << res.first << " Avg time = " << res.second 58 << " ms" << std::endl; 59 } 60