免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: huangyacong
打印 上一主题 下一主题

PYthon的循环,简直是垃圾啊。 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2012-04-19 18:13 |只看该作者
print放在循环里?
好像以前看到说这种测试不科学,因为print非常耗时。

论坛徽章:
0
12 [报告]
发表于 2012-04-19 18:15 |只看该作者
回复 11# anonymous0502


    是的,只是简单的测试。无法可依忽略不计,即使计算上了,不至于每秒1000次吧

论坛徽章:
0
13 [报告]
发表于 2012-04-19 18:25 |只看该作者
本帖最后由 huangyacong 于 2012-04-19 18:38 编辑

回复 10# timespace


    #include "stdafx.h"
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
        int total_count = 0;// 总共执行的次数
        int cur_count = 0; // 当前记录的次数
        DWORD cur_time = GetTickCount(); // 当前时间
        while(1)
        {
                total_count += 1;
                DWORD now_time = GetTickCount();
                if(now_time > cur_time + 300)
                {
                        int tmp_count = total_count - cur_count;
                        DWORD tmp_time = now_time - cur_time;
                        float oo = (float(tmp_count) * float(1000)) / float(tmp_time);
                        printf("TPS=%f\n",oo);
                        cur_count = total_count;
                        cur_time = now_time;
                }
                Sleep(1);
        }
        return 0;
}

TPS=1000次/s搞错了加上sleep



     


论坛徽章:
0
14 [报告]
发表于 2012-04-19 18:31 |只看该作者
回复 10# timespace


    python 版本,

if __name__ == '__main__':
        import time
        cur_count1 = 0
        count1 = 0
       
        tickcount1 = int(time.time()*1000)
       
        def gettick1(tmp_count):
                #print 'qqqqqq'
                global cur_count1
                global tickcount1
                now_time = int(time.time()*1000)
                #print now_time
                if now_time >= tickcount1 + 300:
                        li_time = now_time - tickcount1
                        li_count = tmp_count - cur_count1
                        li = float(li_count) / (float(li_time)/float(1000))
                        print 'TPS=',li,tmp_count
                        tickcount1 = now_time
                        cur_count1 = tmp_count
                       
        while 1:
                time.sleep(0.001)
                count1 = count1 + 1
                gettick1(count1)

TPS = 983次每秒..
那个time.time()不是很精确,不过也够了

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
15 [报告]
发表于 2012-04-19 18:35 |只看该作者
回复 13# huangyacong
usleep或等价的调用在哪里?



   

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
16 [报告]
发表于 2012-04-19 18:36 |只看该作者
回复 14# huangyacong
Python的结果在预期范围,没有争议
重点把C的版本给我看下


   

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
17 [报告]
发表于 2012-04-19 18:38 |只看该作者
莫非Windows能实现1微妙的sleep,我很好奇。。

论坛徽章:
0
18 [报告]
发表于 2012-04-19 21:57 |只看该作者
学习ing

论坛徽章:
0
19 [报告]
发表于 2012-04-20 15:31 |只看该作者
>>> def fs(s):
...     i=0
...     min=0.001
...     intv=1.0/s
...     st=time.time()
...     nt=st
...     while 1:
...             et=time.time()
...             if et-st<intv*i:
...                     time.sleep(min)
...                     continue
...             i+=1
...             if et-st>1:
...                     break
...     print i
...
>>> fs(1)
2
>>> fs(10)
11
>>> fs(100)
101
>>> fs(1000)
1001
>>> fs(10000)
9993
>>> fs(100000)
99938
>>> fs(1000000)
308862
>>>
-------------------------------------------------
以上这段代码可以用来控制代码的执行频率. 当然是不可能靠率精度了, 但是在运行的例子中,可以控制运行次数10w这个粒度上.

当然,如果你想严格做到每个0.01ms执行一次代码, 非实时系统不可能提供这种精度的.

论坛徽章:
0
20 [报告]
发表于 2012-04-20 15:52 |只看该作者
The current implementation of nanosleep() is based on the normal kernel timer mechanism, which has a resolution of 1/HZ

更高级的nanosleep 也只能精确到 1/HZ, 也就是说有0.001s已经很不错了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP