From b7ed1ac13a2b156bde7b1dd52be80803089097e6 Mon Sep 17 00:00:00 2001 From: -m Date: Thu, 18 Apr 2024 14:42:14 +0800 Subject: [PATCH] Update test --- test/ut/mergesort_test.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/test/ut/mergesort_test.cpp b/test/ut/mergesort_test.cpp index 5b45527..d88680a 100644 --- a/test/ut/mergesort_test.cpp +++ b/test/ut/mergesort_test.cpp @@ -4,7 +4,8 @@ using namespace mkr; TEST(mergesort, correctness) { - const int array_size = 10000000; + const int array_size = 1000000 * 1; + const int granularity = 10000; int *unsorted_array = new int[array_size]; // Fill the unsorted array with random numbers. @@ -12,15 +13,17 @@ TEST(mergesort, correctness) { unsorted_array[i] = (int(std::rand()) % array_size); } - std::cout << "Number of Hardware Threads Your System Supports: " << std::thread::hardware_concurrency() << "\n" << std::endl; + std::cout << "Number of Concurrent Threads Your System Supports: " << std::thread::hardware_concurrency() << "\n" << std::endl; // Single Thread int *temp_buffer = new int[array_size]; int *st_sorted_array = new int[array_size]; int *tp_sorted_array = new int[array_size]; + int* sa_sorted_array = new int[array_size]; std::memcpy(&st_sorted_array[0], &unsorted_array[0], array_size * sizeof(unsorted_array[0])); std::memcpy(&tp_sorted_array[0], &unsorted_array[0], array_size * sizeof(unsorted_array[0])); + std::memcpy(&sa_sorted_array[0], &unsorted_array[0], array_size * sizeof(unsorted_array[0])); { std::cout << "Merge Sort " << array_size << " Numbers (Single Thread)" << std::endl; @@ -39,7 +42,19 @@ TEST(mergesort, correctness) { std::cout << "Merge Sort " << array_size << " Numbers (mkr::thread_pool - " << tp.num_threads() << " Threads)" << std::endl; auto start_time = std::chrono::high_resolution_clock::now(); - mergesort_test::thread_pool_mergesort(&tp_sorted_array[0], &temp_buffer[0], 0, array_size, &tp, 500); + mergesort_test::thread_pool_mergesort(&tp_sorted_array[0], &temp_buffer[0], 0, array_size, &tp, granularity); + auto end_time = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(end_time - start_time).count(); + + std::cout << "Time Taken: " << duration << "ms" << std::endl << std::endl; + } + + // std::async + { + std::cout << "Merge Sort " << array_size << " Numbers (std::async)" << std::endl; + + auto start_time = std::chrono::high_resolution_clock::now(); + mergesort_test::async_mergesort(&sa_sorted_array[0], &temp_buffer[0], 0, array_size, granularity); auto end_time = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(end_time - start_time).count(); @@ -51,12 +66,16 @@ TEST(mergesort, correctness) { if (tp_sorted_array[i] != st_sorted_array[i]) { result = false; break; + } else if (sa_sorted_array[i] != st_sorted_array[i]) { + result = false; + break; } } delete[] temp_buffer; delete[] tp_sorted_array; delete[] st_sorted_array; + delete[] sa_sorted_array; EXPECT_TRUE(result); } \ No newline at end of file