如何在C#中更有效地创建300个秒表?

时间:2019-01-04 12:31:25

标签: c# uwp stopwatch

我正在创建一个UWP应用程序,该应用程序需要测量用户查看对象的时间。该应用程序具有约300个对象来测量时间。为此,我们将使用约300个计时器。为此,我们将不得不分别创建300个秒表,这效率非常低。

当用户看着相应的对象时计时器开始计时,而当用户不再看着相应的对象时计时器停止。如果用户的视线再次固定在相应的对象上,则计时器当然会再次启动。最后,所有秒表的时间都将保存到文件中。创建300个秒表需要为每个秒表添加一行新代码,这看起来效率不高。我试图通过使用Enumerable.range自动执行秒表创建过程,但到目前为止,我还没有找到解决方法。

    /// <summary>
    /// All stopwatches for every word. In our real code we will have around 300 stopwatches.
    /// </summary>
    Stopwatch Stopwatch1 = new Stopwatch();
    Stopwatch Stopwatch2 = new Stopwatch();
    Stopwatch Stopwatch3 = new Stopwatch();
    Stopwatch Stopwatch4 = new Stopwatch();
    Stopwatch Stopwatch5 = new Stopwatch();
    Stopwatch Stopwatch6 = new Stopwatch();
    Stopwatch Stopwatch7 = new Stopwatch();
    Stopwatch Stopwatch8 = new Stopwatch();
    Stopwatch Stopwatch9 = new Stopwatch();
    Stopwatch Stopwatch10 = new Stopwatch();

3 个答案:

答案 0 :(得分:2)

因此,首先,您需要获取可用对象的列表。您可以使用以下代码创建通用字典,该字典为您拥有的每个对象保留秒表。还有一种生成注视调查的方法的示例实现。

您仍然必须添加调用start和stop方法的代码。

class Program
{
    static Dictionary<object, Stopwatch> stopwatchesByObject;

    static void Main(string[] args)
    {
        List<object> objects = new List<object>();

        // now you have to fill the objects list...

        stopwatchesByObject = new Dictionary<object, Stopwatch>();
        foreach (var o in objects)
        {
            stopwatchesByObject.Add(o, new Stopwatch());
        }
    }

    // Call this method when the user starts gazing at object o
    static void StartGazingAt(object o)
    {
        stopwatchesByObject[o].Start();
    }

    // Call this method when the user stops gazing at object o
    static void StopGazingAt(object o)
    {
        stopwatchesByObject[o].Stop();
    }

    static void CreateStatistics()
    {
        StringBuilder sb = new StringBuilder();
        foreach (var entry in stopwatchesByObject)
        {
            sb.AppendLine($"Gazed at {entry.Key.ToString()} for {entry.Value.Elapsed.TotalSeconds} seconds.");
        }
        File.WriteAllText("c:\\temp\\statictics.txt", sb.ToString());
    }
}

答案 1 :(得分:1)

我喜欢Stefan Illners的方法,并想添加一些linq-magic,imo确实很整洁,可读性强。

using System.Linq;

var myListOfItemsToWatch = new List<object> { "foo", "bar", "baz" };
var listOfStopwatches = myListOfItemsToWatch.ToDictionary(watchItem =>
        watchItem, i => new Stopwatch()
);

答案 2 :(得分:0)

仅定义List<Stopwatch>()

var l = new List<Stopwatch>();
     for (var i = 0; i < 300; i++)
     {
         var w = Stopwatch.StartNew();

         w.Stop();
         l.Add(w);
     }

要向您展示如何处理它,请参见以下示例

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public static List<Stopwatch> sws = new List<Stopwatch>();
        public static List<Thread> threads = new List<Thread>();

        static void Main()
        {
            for (var i = 0; i < 300; i++)
            {               
                threads.Add(new Thread(Dummy));
                sws.Add(new Stopwatch());
            }

            for(int i = 0; i < 300; i++) sws[i].Start();

            new Thread( () => { for(int i = 0; i < 5; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 5; i < 10; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 10; i < 15; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 15; i < 20; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 20; i < 25; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 25; i < 30; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 30; i < 35; i++) threads[i].Start(i); }).Start();
            new Thread( () => { for(int i = 35; i < 40; i++) threads[i].Start(i); }).Start();

            Console.Read();
        }

        static void Dummy(object data)
        {
            int i = (int)data;
            Thread.Sleep(250); 
            sws[i].Stop();
            Console.WriteLine(sws[i].ElapsedMilliseconds.ToString());
        }
    }
}