C# 为什么在数组搜索场景C中运行时不同#

C# 为什么在数组搜索场景C中运行时不同#,c#,performance-testing,performancecounter,C#,Performance Testing,Performancecounter,我目前正在测试数组中数字搜索的平均时间。我正在测试二进制搜索、顺序索引搜索和自定义函数。使用随机长度和随机值初始化数组。我在测试时注意到两件事 1.)在下面的程序循环中,我的自定义函数大约比二进制搜索快5-7倍,具体取决于数组大小。通常情况下,当阵列更大时(这是目标),速度会快得多 static void Main(string[] args) { int[] randomarray = CreateRandomArray(); for(int loops = 0;loops &

我目前正在测试数组中数字搜索的平均时间。我正在测试二进制搜索、顺序索引搜索和自定义函数。使用随机长度和随机值初始化数组。我在测试时注意到两件事

1.)在下面的程序循环中,我的自定义函数大约比二进制搜索快5-7倍,具体取决于数组大小。通常情况下,当阵列更大时(这是目标),速度会快得多

static void Main(string[] args)
{
    int[] randomarray = CreateRandomArray();
    for(int loops = 0;loops < totalsamples;loops ++)
    {
        int tosearchfor = CreateRandomNumber();
        RecordMyCustomFunctionTime(randomarray,tosearchfor);
        RecordBinarySearchTime(randomarray,tosearchfor);
        RecordSequentialSearchTime(randomarray,tosearchfor);
    }
    CalculateAndDisplayAverages();
}
static void Main(字符串[]args)
{
int[]randomarray=CreateRandomArray();
for(int循环=0;循环
2.)在下面的程序循环中,根据阵列大小,我的自定义函数大约快2-3倍。二进制搜索和顺序搜索与以前大致相同

static void Main(string[] args)
{
    for(int loops = 0;loops < totalsamples;loops ++)
    {
        int[] randomarray = CreateRandomArray();
        int tosearchfor = CreateRandomNumber();
        RecordMyCustomFunctionTime(randomarray,tosearchfor);
        RecordBinarySearchTime(randomarray,tosearchfor);
        RecordSequentialSearchTime(randomarray,tosearchfor);
    }
    CalculateAndDisplayAverages();
}
static void Main(字符串[]args)
{
for(int循环=0;循环

我还使用设置的数组大小对此进行了测试,得到了相同的结果。我正在使用Stopwatch类,它报告我有一个高分辨率性能计数器。为什么这样的差异仅仅取决于数组的创建位置和我的函数?

内存分配是一项繁重的任务,需要时间。@Idov它不在测试时间开始到测试时间结束的时间范围内,弹性时间是在每次函数调用和返回时测量的。我百分之百确定它没有被计算进去。你必须给我们看更多的代码。。。