コード例 #1
0
        /// <summary>
        ///     Initializes a new instance of the CodeTimer class.
        /// </summary>
        /// <param name="startFresh">
        ///     If true, forces a GC in order to count just new garbage collections.
        /// </param>
        /// <param name="format">
        ///     A composite text string.
        /// </param>
        /// <param name="args">
        ///     An array of objects to write with <paramref name="format" />.
        /// </param>
        CodeTimer(bool startFresh,
                  string format,
                  params object[] args)
        {
            if (startFresh)
            {
                PrepareForOperation();
            }

            this.testText = string.Format(format, args);

            this.gen0Start = GC.CollectionCount(0);
            this.gen1Start = GC.CollectionCount(1);
            this.gen2Start = GC.CollectionCount(2);

            // Get the time before returning so that any code above doesn't
            // impact the time.
            this.startTime   = Stopwatch.GetTimestamp();
            this.startCycles = CycleTime.Thread();
        }
コード例 #2
0
        public void Dispose()
        {
            ulong elapsedCycles = CycleTime.Thread() - this.startCycles;

            // Get the elapsed time now so that any code below doesn't impact
            // the time.
            long elapsedTime = Stopwatch.GetTimestamp() - this.startTime;

            long milliseconds = (elapsedTime * 1000) / Stopwatch.Frequency;

            if (false == string.IsNullOrEmpty(this.testText))
            {
                ConsoleColor defColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("{0}", this.testText);
                Console.ForegroundColor = defColor;
                Console.WriteLine("   {0,7:N0}ms {1,11:N0}Kc (G0={2,4}, G1={3,4}, G2={4,4})",
                                  milliseconds,
                                  elapsedCycles / 1000,
                                  GC.CollectionCount(0) - this.gen0Start,
                                  GC.CollectionCount(1) - this.gen1Start,
                                  GC.CollectionCount(2) - this.gen2Start);
            }
        }