Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Linq比PHP慢?解谜#236A_C#_Php_Linq - Fatal编程技术网

C#Linq比PHP慢?解谜#236A

C#Linq比PHP慢?解谜#236A,c#,php,linq,C#,Php,Linq,我正在一个网站上接受解决奥运会IT谜题的培训 我提供了两种解决方案: - C# 当在线判断显示PHP版本更快时,我很困惑!!! 为什么? C#:109毫秒3000 Kb PHP:45毫秒0 Kb 怎么可能呢 您的C#版本创建了三个您似乎不需要的数组。您可以将其替换为: string input = Console.ReadLine(); int charCount = input.Distinct().Count(); if(charCount % 2 == 0) ... 以下可能更

我正在一个网站上接受解决奥运会IT谜题的培训

我提供了两种解决方案:

 - C# 

当在线判断显示PHP版本更快时,我很困惑!!! 为什么?

C#:109毫秒3000 Kb

PHP:45毫秒0 Kb

怎么可能呢

您的C#版本创建了三个您似乎不需要的数组。您可以将其替换为:

string input = Console.ReadLine();
int charCount = input.Distinct().Count();
if(charCount % 2 == 0) ...
以下可能更快:

int charCount = new HashSet<char>(input).Count;
int charCount=新哈希集(输入).Count;

给定给定的程序,程序的重要部分(查找唯一字符)的执行时间肯定不会花费109ms。听起来,无论涉及到什么“在线判断”,都是在衡量总执行时间,包括进程启动、.NET情况下的JITting等

这有点像问哪辆车从车库出来更快,然后想这代表了汽车的速度

现在完全有可能PHP的
array\u unique
函数真的非常快,可能比LINQ快。。。但基本上,您无法从基准测试结果中获得任何有用的信息。您应该寻找执行时间为秒而不是毫秒的基准,并且不包括启动/预热时间,除非您特别感兴趣。

事实上,“0kB”方面也相当令人印象深刻!
int charCount = new HashSet<char>(input).Count;