zoukankan      html  css  js  c++  java
  • Thread.Sleep和Timer性能比较

    在一些需要隔时触发的场景中,如javascript中的setInterval函数,在.Net中,你用什么?

    是System.Timer.Timer?

    or

    while(true)

    {

         Thread.Sleep(1000);

    }

    今天比较一下Timer和Sleep.

    结果:

         Thread.sleep,问天下谁于争峰。

    空间:

    Type work set virtual bytes page file bytes Thread Count Handle
    Timer 8.990.720 114.978.816 11.444.224 4 115
    Thread.Sleep 6.590464 104.296.448 7.143.424 3 95

     Thread.Sleep全胜

    时间:

    Type start Time Interval times finish time
    Timer 11:53:37:416 10ms 100000 12:19:37:555
    Thread.Sleep 11:53:37:432 10ms 100000 12:19:36:713

       Timer和Thread.Sleep打个平手,但Thread.sleep还是要强那么一点

    分析:

         Thread.sleep直接调用内核的指令,所在线程挂起,CPU执行队列的重排序。

         Timer每次Elapsed会在线程池中取新的线程来执行,存在多次访问线程池的损耗。

    测试代码:

    Code
  • 相关阅读:
    iPhone开发应用Sqlite使用手册
    2.23 Apps must follow the iOS Data Storage Guidelines or they will be rejected
    跨浏览器(IE/FF/OPERA)JS代码小结
    c#一次数据库查询,JS实现内容分页
    oracle PLSQL /sqlserver2005基本操作对比
    SqlParameter构造函数的临界边缘
    SQL SERVER 2005分页存储过程
    *自创*可变长度随机数字/字母的生成小结(针对文件上传及验证码)
    Visual Source Safe连接数据文件图解 解决密码缓存问题
    [Ubuntu] Invalid command 'VirtualDocumentRoot', perhaps misspelled or defined by a module not included in the server configuration
  • 原文地址:https://www.cnblogs.com/DataFlow/p/1408731.html
Copyright © 2011-2022 走看看