I'm trying to compare the performance of the standard sorting facilities of .Net and C++, just for fun mainly. I devised the following test in C++/CLI: to run this, create a "CLR Console Application". I just fill a large C++ array and an equally large .NET array with the same random numbers, and sort each with their respective standard sorting functions: std::sort and Array.Sort, respectively. I repeat the test a few times and compute the average for each.
#include "stdafx.h" #include <array> #include <algorithm> using namespace System; using namespace System::Diagnostics;
const int ARRAY_SIZE = 1000000; const int NUM_LOOPS = 20;
// Testing .Net Array.Sort() vs C++'s std::sort on both languages standard arrays. int main(array<System::String ^> ^args) { auto cppArray = new int[ARRAY_SIZE]; auto netArray = gcnew array<int>(ARRAY_SIZE); double totalTimeCpp = 0.0; double totalTimeNet = 0.0;
auto randGen = gcnew Random();
for (int i = 0; i < NUM_LOOPS; ++i) { for (int i = 0; i < ARRAY_SIZE; ++i) { int randNum = randGen->Next(); cppArray[i] = randNum; netArray[i] = randNum; }
Console::WriteLine(L"Average time C++: {0} milliseconds.", totalTimeCpp / (double)NUM_LOOPS); Console::WriteLine(L"Average time .NET: {0} milliseconds.", totalTimeNet / (double)NUM_LOOPS); Console::WriteLine(L"Array.Sort time / std::sort time: {0}.", totalTimeNet / totalTimeCpp); Console::ReadKey(false); return 0; }[/CODE]
I'm posting this because I can't believe the results. In release mode, optimizing for speed, Array.Sort() takes 89.6% of the time std::sort() takes on my machine. std::sort is supposed to be faster, if only because C++ doesn't perform bounds check on each random access in an array and .Net does. At this point I suspect there's something wrong with my testing methodology, perhaps due to compiling with the /CLI switch on? I don't know, so if you have a better idea let me know.
Question
Andre S. Veteran
I'm trying to compare the performance of the standard sorting facilities of .Net and C++, just for fun mainly. I devised the following test in C++/CLI: to run this, create a "CLR Console Application". I just fill a large C++ array and an equally large .NET array with the same random numbers, and sort each with their respective standard sorting functions: std::sort and Array.Sort, respectively. I repeat the test a few times and compute the average for each.
I'm posting this because I can't believe the results. In release mode, optimizing for speed, Array.Sort() takes 89.6% of the time std::sort() takes on my machine. std::sort is supposed to be faster, if only because C++ doesn't perform bounds check on each random access in an array and .Net does. At this point I suspect there's something wrong with my testing methodology, perhaps due to compiling with the /CLI switch on? I don't know, so if you have a better idea let me know.
Link to comment
https://www.neowin.net/forum/topic/1057418-c-vs-c-performance-sorting-an-array/Share on other sites
17 answers to this question
Recommended Posts