代码之家  ›  专栏  ›  技术社区  ›  user366312

C语言中整数除法与双除法#

  •  0
  • user366312  · 技术社区  · 4 年前
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;    
    
    namespace MainProgram
    {
        static class Program
        {
            static void Main()
            {
                const int intCount = 9999999;
                const double doubleCount = (double)intCount;
    
                List<int> integerList = new List<int>();
                List<double> doubleList = new List<double>();
    
                Random r = new Random();
    
                for (int i = 0; i < intCount; i++)
                {
                    int intt = r.Next(1, 100);
    
                    integerList.Add(intt);
                    doubleList.Add(intt);
                }
    
                Stopwatch intWatch = new Stopwatch();
    
                intWatch.Start();
    
                for (int i = 0; i < intCount; i++)
                {
                    integerList[i] = integerList[i] / intCount;
                }
    
                intWatch.Stop();
    
                Console.WriteLine($"Int Execution Time: {intWatch.ElapsedMilliseconds} ms");
    
                ///////////////////////////////////////////
                Stopwatch doubleWatch = new Stopwatch();
    
                doubleWatch.Start();
    
                for (int i = 0; i < doubleCount; i++)
                {
                    doubleList[i] = doubleList[i] / doubleCount;
                }
    
                doubleWatch.Stop();
    
                Console.WriteLine($"Double Execution Time: {doubleWatch.ElapsedMilliseconds} ms");
    
                Console.ReadLine();
            }
        }
    }
    

    输出:

    Int Execution Time: 114 ms
    Double Execution Time: 108 ms
    

    我有两个问题:

    1. 此测试代码是否反映了正确的测试代码?
    2. 如果测试代码是正确的,为什么double类型花费的时间更少?

    // * Summary *
    
    BenchmarkDotNet=v0.10.12, OS=Windows 10.0.18362
    Intel Core i7-3520M CPU 2.90GHz (Ivy Bridge), 1 CPU, 4 logical cores and 2 physical cores
      [Host]     : .NET Framework 4.6.1 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.8.4220.0  [AttachedDebugger]
      DefaultJob : .NET Framework 4.6.1 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.8.4220.0
    
    
     Method |     Mean |      Error |     StdDev |   Median | Rank |     Gen 0 |     Gen 1 |     Gen 2 | Allocated |
    ------- |---------:|-----------:|-----------:|---------:|-----:|----------:|----------:|----------:|----------:|
      Test1 | 375.3 ms |  0.7270 ms |  0.6801 ms | 375.2 ms |    1 | 3937.5000 | 3937.5000 | 3937.5000 | 128.01 MB |
      Test2 | 509.6 ms | 10.6781 ms | 30.1178 ms | 496.5 ms |    2 | 4937.5000 | 4937.5000 | 4937.5000 | 256.01 MB |
    
    // * Warnings *
    Environment
      Summary -> Benchmark was executed with attached debugger
    
    // * Hints *
    Outliers
      DateParserBenchmark.Test2: Default -> 7 outliers were removed
    
    // * Legends *
      Mean      : Arithmetic mean of all measurements
      Error     : Half of 99.9% confidence interval
      StdDev    : Standard deviation of all measurements
      Median    : Value separating the higher half of all measurements (50th percentile)
      Rank      : Relative position of current benchmark mean among all benchmarks (Arabic style)
      Gen 0     : GC Generation 0 collects per 1k Operations
      Gen 1     : GC Generation 1 collects per 1k Operations
      Gen 2     : GC Generation 2 collects per 1k Operations
      Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
      1 ms      : 1 Millisecond (0.001 sec)
    
    // * Diagnostic Output - MemoryDiagnoser *
    
    
    // ***** BenchmarkRunner: End *****
    // * Artifacts cleanup *
    

    Test1() 是整数, Test2() 是双倍的。

    0 回复  |  直到 4 年前