我决定在排序测试中将速度C++ std :: vector与C#List进行比较。 所以我给他们填充了2 700 000个相同的字符串并测量了排序的时间。排序C++向量和C#列表
它看起来像这样:
C++:
std::vector<std::string> CPPList;
std::ifstream file("words-back.txt");
std::string word;
while(std::getline(file, word))
{
CPPList.push_back(word);
}
file.close();
Profiler profiler;
profiler.Start();
std::sort(CPPList.begin(),CPPList.end());
profiler.Stop();
C#:
string[] lista = File.ReadAllLines("words-back.txt").ToArray();
List<string> CSList = new List<string>();
foreach (string element in lista)
{
CSList.Add(element);
}
Stopwatch timer = Stopwatch.StartNew();
CSList.Sort();
timer.Stop();
和结果出乎我的意料,以至于我不得不问你到底是怎么回事。 C++需要0.7秒,而C#需要25秒。我确实输出了排序字符串的文件,以确保排序正确完成并且是正确的。
我的问题是:为什么C#使这个比C++长得多。
我很抱歉,我才吃了一个零它不是270 000,但2 700 000字符串在这个文件中,
C++比C#更快吗?这很奇怪... –
请显示您的实际**完整**代码。你是否也在测量JIT时间? – SLaks
虽然问题是什么? – Arun