当前位置: 首页>>代码示例>>C#>>正文


C# Stopwatch.Restart方法代码示例

本文整理汇总了C#中Stopwatch.Restart方法的典型用法代码示例。如果您正苦于以下问题:C# Stopwatch.Restart方法的具体用法?C# Stopwatch.Restart怎么用?C# Stopwatch.Restart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Stopwatch的用法示例。


在下文中一共展示了Stopwatch.Restart方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

    internal static void Run()
    {
        var f = new Form() { Width = 400, Height = 300 };
        var b = new Button() { Text = "Run", Dock = DockStyle.Fill, Font = new Font("Consolas", 18) };
        f.Controls.Add(b);

        b.Click += async delegate
        {
            b.Text = "... Running ... ";
            await Task.WhenAll(WithSyncCtx(), WithoutSyncCtx()); // warm-up

            var sw = new Stopwatch();

            sw.Restart();
            await WithSyncCtx();
            var withTime = sw.Elapsed;

            sw.Restart();
            await WithoutSyncCtx();
            var withoutTime = sw.Elapsed;

            b.Text = string.Format("With    : {0}\nWithout : {1}\n\nDiff    : {2:F2}x", 
                withTime, withoutTime, withTime.TotalSeconds / withoutTime.TotalSeconds);
        };

        f.ShowDialog();
    }
开发者ID:debop,项目名称:NFramework,代码行数:27,代码来源:5.CapturingContext.cs

示例2: Main

    static void Main()
    {
        var context = new SoftUniEntities();

        // Established connection with the base
        //var count = context.Employees.Count();
        //Console.WriteLine(count);

        //for (int image = 0; image < 1000; image++)
        //{
        //    context.Images.Add(new Image()
        //    {
        //        ImageBase64 = new string('-', 10) + image
        //    });

        //    Console.WriteLine(image);
        //}

        //context.SaveChanges();

        var sw = new Stopwatch();
        sw.Start();
        EfExtensionsDelete(context);
        Console.WriteLine("Standart: {0}", sw.Elapsed);

        sw.Restart();
        NativeDelete(context);
        Console.WriteLine("Native: {0}", sw.Elapsed);

        sw.Restart();
        EfStandartDelete(context);
        Console.WriteLine("Extensions: {0}", sw.Elapsed);
    }
开发者ID:DimitarStoyanovVictory,项目名称:DataBase-Apps,代码行数:33,代码来源:Program.cs

示例3: Main

    static void Main()
    {
        Stopwatch watch = new Stopwatch();

        // SQRT
        Console.WriteLine("SQRT:");
        watch.Restart();
        Sqrt.FloatSqrt(1f, 10000000f, 1f);
        watch.Stop();
        Console.WriteLine("Float: " + watch.ElapsedMilliseconds);

        watch.Restart();
        Sqrt.DoubleSqrt(1d, 10000000d, 1d);
        watch.Stop();
        Console.WriteLine("Double: " + watch.ElapsedMilliseconds);

        watch.Restart();
        Sqrt.DecimalSqrt(1m, 10000000m, 1m);
        watch.Stop();
        Console.WriteLine("Decimal: " + watch.ElapsedMilliseconds);

        // Sinus
        Console.WriteLine("Sinus:");
        watch.Restart();
        Sinus.FloatSinus(1f, 10000000f, 1f);
        watch.Stop();
        Console.WriteLine("Float: " + watch.ElapsedMilliseconds);

        watch.Restart();
        Sinus.DoubleSinus(1d, 10000000d, 1d);
        watch.Stop();
        Console.WriteLine("Double: " + watch.ElapsedMilliseconds);

        watch.Restart();
        Sinus.DecimalSinus(1m, 10000000m, 1m);
        watch.Stop();
        Console.WriteLine("Decimal: " + watch.ElapsedMilliseconds);

        // Logarithm
        Console.WriteLine("Logarithm:");
        watch.Restart();
        Logarithm.FloatLogarithm(1f, 10000000f, 1f);
        watch.Stop();
        Console.WriteLine("Float: " + watch.ElapsedMilliseconds);

        watch.Restart();
        Logarithm.DoubleLogarithm(1d, 10000000d, 1d);
        watch.Stop();
        Console.WriteLine("Double: " + watch.ElapsedMilliseconds);

        watch.Restart();
        Logarithm.DecimalLogarithm(1m, 10000000m, 1m);
        watch.Stop();
        Console.WriteLine("Decimal: " + watch.ElapsedMilliseconds);
    }
开发者ID:nkolarov,项目名称:telerik-academy,代码行数:55,代码来源:JustAnotherTest.cs

示例4: Main

 static void Main()
 {
     TelerikAcademyEntities db = new TelerikAcademyEntities();
     Stopwatch sw = new Stopwatch();
     using (db)
     {
         sw.Start();
         IEnumerable query = db.Employees.ToList()
              .Select(x => x.Address).ToList()
              .Select(t => t.Town).ToList()
              .Where(t => t.Name == "Sofia");
         sw.Stop();
         Console.WriteLine("Slow: {0}", sw.Elapsed);
         // made 644 queries
         sw.Restart();
         
         IEnumerable querySmart = db.Employees
           .Select(x => x.Address)
           .Select(t => t.Town)
           .Where(t => t.Name == "Sofia").ToList();
         sw.Stop();
         Console.WriteLine("Fast: {0}", sw.Elapsed);
        // made 2 queries
     }
 }
开发者ID:krstan4o,项目名称:TelerikAcademy,代码行数:25,代码来源:Program.cs

示例5: Main

    static void Main(string[] args)
    {
        int[] oPos = new int[4];//0to1 - position, 2-3 - other status
        string[,] screen = new string[25, 55];
        int[,] projectiles = new int[25, 55];
        int difficulty = 420;

        //fills screen with space
        for (int i = 0; i < 25; i++)
        {
            for (int j = 0; j < 55; j++)
            {
                screen[i, j] = " ";

            }

        }

        for (int i = 0; i < 25; i++)
        {
            for (int j = 0; j < 55; j++)
            {

                projectiles[i, j] = 0;

            }
        }

        oPos[0] = 24;//i
        oPos[1] = 22;//j
        Stopwatch highScoreSw = new Stopwatch();
        Stopwatch diffSw = new Stopwatch();
        diffSw.Start();
        highScoreSw.Start();
        while (true)
        {
            if (diffSw.Elapsed.Seconds.CompareTo(5) == 1 && difficulty != 20
                && diffSw.Elapsed.Seconds < 35)
            {
                difficulty -= 50;
                diffSw.Restart();
            }
            Console.Clear();

            GetInput(oPos);
            ProjMovementAndCreation(projectiles, difficulty);
            DrawScreen(screen, projectiles, oPos, highScoreSw);
            CollisionDetection(projectiles, oPos, ref difficulty);

            Thread.Sleep(100);
        }


    }
开发者ID:Dyno1990,项目名称:TelerikAcademy-1,代码行数:54,代码来源:FallingRocks.cs

示例6: Main

    // Note: I have referenced the AdsEntities from the previous project
    // instead of creating another one in this project.
    static void Main()
    {
        /*
         * Using Entity Framework select all ads from the database,
         * then invoke ToList(), then filter the categories whose status is Published;
         * then select the ad title, category and town, then invoke ToList()
         * again and finally order the ads by publish date.
         * Rewrite the same query in a more optimized way and compare the performance.
         */

        var context = new AdsEntities();

        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int count = 0; count < 10; count++)
        {
            var ads = context.Ads
            .Select(a => a)
            .ToList()
            .Where(a => a.AdStatus != null && a.AdStatus.Status == "Published")
            .Select(a => new
            {
                Title = a.Title,
                Category = a.Category != null ? a.Category.Name : "(null)",
                Town = a.Town != null ? a.Town.Name : "(null)",
                PublishDate = a.Date
            })
            .ToList()
            .OrderBy(a => a.PublishDate);
        }

        // Unoptimised: 5 seconds
        Console.WriteLine("Unoptimised: {0}", sw.Elapsed);
        sw.Restart();

        for (int count = 0; count < 10; count++)
        {
            var ads = context.Ads
            .Where(a => a.AdStatus != null && a.AdStatus.Status == "Published")
            .Select(a => new
            {
                Title = a.Title,
                Category = a.Category != null ? a.Category.Name : "(null)",
                Town = a.Town != null ? a.Town.Name : "(null)",
                PublishDate = a.Date
            })
            .OrderBy(a => a.PublishDate)
            .ToList();
        }

        // Optimised: 0.5 seconds
        Console.WriteLine("Optimised: {0}", sw.Elapsed);
        sw.Stop();
    }
开发者ID:dev-velkov,项目名称:Software-University-Courses,代码行数:56,代码来源:ToListOptimisation.cs

示例7: IncrementOccuranceCountTrie

    private static void IncrementOccuranceCountTrie(Stopwatch sw, TrieNode start, MatchCollection allWords)
    {
        sw.Restart();
        foreach (var word in allWords)
        {
            start.AddOccuranceIfExists(start, word.ToString());
        }

        sw.Stop();
        Console.WriteLine("Adding searched words count trie for: {0}", sw.Elapsed);
    }
开发者ID:KirilToshev,项目名称:Projects,代码行数:11,代码来源:FindWordsInText.cs

示例8: Main

    static void Main()
    {
        var context = new AdsEntities1();

        var stopWatch = new Stopwatch();
        stopWatch.Start();
        NotOptimizedMethod(context);
        Console.WriteLine("Not optimized method: {0}", stopWatch.Elapsed);
        stopWatch.Restart();
        OptimizedMethod(context);
        Console.WriteLine("Optimized method: {0}", stopWatch.Elapsed);
    }
开发者ID:DimitarStoyanovVictory,项目名称:DataBase-Apps,代码行数:12,代码来源:Test.cs

示例9: SendMessage

 public static void SendMessage(int seconds)
 {
     Stopwatch sw = new Stopwatch();
     sw.Start();
     while (true)
     {
         if (sw.ElapsedMilliseconds == seconds * 1000)
         {
             Console.WriteLine("Surprise");
             sw.Restart();
         }
     }
 }
开发者ID:hristo11111,项目名称:TelerikAcademy-HristoBratanov,代码行数:13,代码来源:Program.cs

示例10: startTimer

 public void startTimer(int t)
 {
     Stopwatch sw = new Stopwatch();
     sw.Start();
     while (true)
     {
         if (sw.ElapsedMilliseconds >= t * 1000)
         {
             Console.WriteLine("Boom");
             sw.Restart();
         }
     }
 }
开发者ID:valkanov,项目名称:TelerikAcademy,代码行数:13,代码来源:Timer.cs

示例11: Run

    internal static void Run()
    {
        var sw = new Stopwatch();
        while (true)
        {
            CallContext.LogicalSetData("Foo", "Bar"); // changes from default context

            sw.Restart();
            DoWorkAsync().Wait();
            var withTime = sw.Elapsed;

            CallContext.FreeNamedDataSlot("Foo"); // back to default context

            sw.Restart();
            DoWorkAsync().Wait();
            var withoutTime = sw.Elapsed;

            Console.WriteLine("With    : {0}", withTime);
            Console.WriteLine("Without : {0}", withoutTime);
            Console.WriteLine("---- {0:F2}x ----", withTime.TotalSeconds / withoutTime.TotalSeconds);
            Console.ReadLine();
        }
    }
开发者ID:debop,项目名称:NFramework,代码行数:23,代码来源:6.FlowingContext.cs

示例12: AddWordsForSearchInDictionary

 private static void AddWordsForSearchInDictionary(Stopwatch sw, List<string> words, Dictionary<string, int> wordsInDictionary)
 {
     sw.Restart();
         foreach (var item in words)
         {
             string word = item.ToString();
             if (!wordsInDictionary.ContainsKey(word))
             {
                 wordsInDictionary[word] = 0;
             }
         }
         sw.Stop();
         Console.WriteLine("Time to populate dictionary: {0}\n\n", sw.Elapsed);
 }
开发者ID:KirilToshev,项目名称:Projects,代码行数:14,代码来源:FindWordsInText.cs

示例13: Main

    static void Main()
    {
        var context = new AdsEntities();
        context.Database.ExecuteSqlCommand("CHECKPOINT; DBCC DROPCLEANBUFFERS;");

        var sw = new Stopwatch();
        Console.WriteLine(context.Ads.Any());

        sw.Start();
        // Messy query
        var ads = context.Ads
            .ToList()
            .Where(a => a.AdStatus.Status == "Published")
            .Select(a => new
            {
                Title = a.Title,
                Category = a.Category,
                Town = a.Town,
                Date = a.Date
            })
            .ToList()
            .OrderBy(a => a.Date);

        Console.WriteLine("Millisecond with a messy query: " + sw.ElapsedMilliseconds + "ms");

        sw.Restart();
        var adsImproved = context.Ads
           .Where(a => a.AdStatus.Status == "Published")
           .Select(a => new
           {
               Title = a.Title,
               Category = a.Category,
               Town = a.Town,
               Date = a.Date
           })
           .OrderBy(a => a.Date)
           .ToList();

        Console.WriteLine("Millisecond with a proper query: " + sw.ElapsedMilliseconds + "ms");

        // TEST RESULTS:
        //+---------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+---------+
        //|               | Run 1 | Run 2 | Run 3 | Run 4 | Run 5 | Run 6 | Run 7 | Run 8 | Run 9 | Run 10 | Average |
        //+---------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+---------+
        //| Non-optimized |  237  |  245  |  243  |  247  |  256  |  237  |  236  |  266  |  236  |  237   |  244ms  |
        //+---------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+---------+
        //| Optimized     |  123  |  122  |  125  |  128  |  121  |  123  |  123  |  121  |  122  |  123   |  123ms  |
        //+---------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+--------+---------+
        // Improvement - Almost 2 (1.98) times faster.
    }
开发者ID:zerefel,项目名称:DatabaseApplications,代码行数:50,代码来源:TestToList.cs

示例14: Run

    internal static void Run()
    {
        var sw = new Stopwatch();
        const int ITERS = 10000000;

        EmptyBody();
        EmptyBodyAsync();

        while (true)
        {
            sw.Restart();
            for (int i = 0; i < ITERS; i++) EmptyBody();
            var emptyBodyTime = sw.Elapsed;

            sw.Restart();
            for (int i = 0; i < ITERS; i++) EmptyBodyAsync();
            var emptyBodyAsyncTime = sw.Elapsed;

            Console.WriteLine("Sync  : {0}", emptyBodyTime);
            Console.WriteLine("Async : {0}", emptyBodyAsyncTime);
            Console.WriteLine("-- {0:F1}x --", emptyBodyAsyncTime.TotalSeconds / emptyBodyTime.TotalSeconds);
        }
    }
开发者ID:debop,项目名称:NFramework,代码行数:23,代码来源:1.EmptyBodyOverhead.cs

示例15: Run

    internal static void Run()
    {
        string url = "http://www.microsoft.com";
        GetContents1Async(url).Wait();
        GetContents2Async(url).Wait();

        var sw = new Stopwatch();
        while (true)
        {
            sw.Restart();
            for (int i = 0; i < ITERS; i++) GetContents1Async(url).Wait();
            var cacheStringTime = sw.Elapsed;

            sw.Restart();
            for (int i = 0; i < ITERS; i++) GetContents2Async(url).Wait();
            var cacheTaskTime = sw.Elapsed;

            Console.WriteLine("Cache string : {0}", cacheStringTime);
            Console.WriteLine("Cache task   : {0}", cacheTaskTime);
            Console.WriteLine("---- {0:F2}x ----", cacheStringTime.TotalSeconds / cacheTaskTime.TotalSeconds);
            Console.ReadLine();
        }
    }
开发者ID:debop,项目名称:NFramework,代码行数:23,代码来源:4.DictionaryCaching.cs


注:本文中的Stopwatch.Restart方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。