Precalculating array length
This commit is contained in:
16
task7.cpp
16
task7.cpp
@@ -6,7 +6,10 @@
|
||||
|
||||
// Короткий и длинный C-массивы
|
||||
int arr_short[] = {5, 7, 1, 2, 15, 29, 100, 1000};
|
||||
int arr_short_len = sizeof(arr_short) / sizeof(arr_short[0]);
|
||||
|
||||
int arr_long[] = {165, 2, 23, 7, 9, 124, 4, 5000, 26, 20, 80, 55, 6, 1, 8, 10, 9, 12, 3, 6, 7, 5, 4, 4, 3, 2, 1, 100, 1000, 10000};
|
||||
int arr_long_len = sizeof(arr_long) / sizeof(arr_long[0]);
|
||||
|
||||
// Короткий и длинный векторы
|
||||
std::vector<int> vec_short = {5, 7, 1, 2, 15, 29, 100, 1000};
|
||||
@@ -22,12 +25,15 @@ double benchmark(void (*func)(int[], int), int arr[], int n)
|
||||
// нам не нужно очищать за собой память (удалять массивы).
|
||||
// Обязательно удаляйте массивы, если используете указатели!
|
||||
int arr_copy[n];
|
||||
std::copy(arr, arr + n, arr_copy);
|
||||
std::copy(arr, arr + n, arr_copy); // arr - исходный, arr + n - конец, arr_copy - новый массив
|
||||
|
||||
// Начинаем отсчет времени, выполняем функцию сортировки,
|
||||
// заканчиваем отсчет.
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
func(arr_copy, n);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// Возвращаем разницу начала и конца в наносекундах
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
|
||||
}
|
||||
|
||||
@@ -59,10 +65,10 @@ double benchmark_vector(void (*func)(std::vector<int>&, int, int), std::vector<i
|
||||
int main()
|
||||
{
|
||||
std::cout << "Bubble sort: " << benchmark_vector(bubbleSort, vec_short) << ", " << benchmark_vector(bubbleSort, vec_long) << std::endl;
|
||||
std::cout << "Cocktail sort: " << benchmark(CocktailSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(CocktailSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl;
|
||||
std::cout << "Insertion sort: " << benchmark(insertionSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(insertionSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl;
|
||||
std::cout << "Selection sort: " << benchmark(selectionSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(selectionSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl;
|
||||
std::cout << "Shell sort: " << benchmark(shellSort, arr_short, sizeof(arr_short) / sizeof(arr_short[0])) << ", " << benchmark(shellSort, arr_long, sizeof(arr_long) / sizeof(arr_long[0])) << std::endl;
|
||||
std::cout << "Cocktail sort: " << benchmark(CocktailSort, arr_short, arr_short_len) << ", " << benchmark(CocktailSort, arr_long, arr_long_len) << std::endl;
|
||||
std::cout << "Insertion sort: " << benchmark(insertionSort, arr_short, arr_short_len) << ", " << benchmark(insertionSort, arr_long, arr_long_len) << std::endl;
|
||||
std::cout << "Selection sort: " << benchmark(selectionSort, arr_short, arr_short_len) << ", " << benchmark(selectionSort, arr_long, arr_long_len) << std::endl;
|
||||
std::cout << "Shell sort: " << benchmark(shellSort, arr_short, arr_short_len) << ", " << benchmark(shellSort, arr_long, arr_long_len) << std::endl;
|
||||
std::cout << "Quick sort: " << benchmark_vector(quickSort, vec_short) << ", " << benchmark_vector(quickSort, vec_long) << std::endl;
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user