All pastes #109084 Raw Edit

aj

public text v1 · immutable
#109084 ·published 2006-07-31 20:54 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;

namespace TestHarness
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Test> list = new List<Test>();

            Dictionary<int, Test> dictionary = new Dictionary<int, Test>();

            Win32.HiPerfTimer hpt = new Win32.HiPerfTimer();

            int count = 0;
            
            hpt.Start();
            
            while (count < 10000)
            {
                list.Add(new Test(count, DateTime.Now.ToString()));
                count++;
            }

            hpt.Stop();

            Console.WriteLine("List insert: " + hpt.Duration);

            hpt = new Win32.HiPerfTimer();

            count = 0;

            hpt.Start();

            while (count < 10000)
            {
                dictionary.Add(count,new Test(count, DateTime.Now.ToString()));
                count++;
            }

            hpt.Stop();

            Console.WriteLine("Dictionary insert: " + hpt.Duration);

            hpt = new Win32.HiPerfTimer();

            hpt.Start();

            count = 0;

            while (count < 10000)
            {
                Test test = list.Find(
                    delegate(Test tst)
                    {
                        return (tst.ID == count);
                    }
                    );

                count++;
            }

            hpt.Stop();

            Console.WriteLine("List find: " + hpt.Duration);

            hpt = new Win32.HiPerfTimer();

            hpt.Start();

            count = 0;

            while (count < 10000)
            {
                Test test = dictionary[count];
                count++;
            }

            hpt.Stop();

            Console.WriteLine("Dictionary find: " + hpt.Duration);

            Console.Read();
        }
    }

    struct Test
    {
        public int ID;
        public string Value;

        public Test(int id, string value)
        {
            this.ID = id;
            this.Value = value;
        }
    }
}


using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace Win32
{
    internal class HiPerfTimer
    {
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(
            out long lpPerformanceCount);

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(
            out long lpFrequency);

        private long startTime, stopTime;
        private long freq;

        // Constructor
        public HiPerfTimer()
        {
            startTime = 0;
            stopTime  = 0;

            if (QueryPerformanceFrequency(out freq) == false)
            {
                // high-performance counter not supported
                throw new Win32Exception();
            }
        }

        // Start the timer
        public void Start()
        {
            // lets do the waiting threads there work
            Thread.Sleep(0);

            QueryPerformanceCounter(out startTime);
        }

        // Stop the timer
        public void Stop()
        {
            QueryPerformanceCounter(out stopTime);
        }

        // Returns the duration of the timer (in seconds)
        public double Duration
        {
            get
            {
                return (double)(stopTime - startTime) / (double) freq;
            }
        }
    }
}