找回密码
 加入
搜索
查看: 5187|回复: 10

[效率算法] 怪異的sleep.試試您的電腦

  [复制链接]
发表于 2011-1-2 10:24:05 | 显示全部楼层 |阅读模式
本帖最后由 yangdai 于 2011-1-2 15:45 编辑

下面的程式,sleep 正常值為1556,
換成Hsleep,在4台不同cpu的桌上電腦測試
1台可以正確跑出預期的97(沒超頻)
1台有時97,有時1556(e8400 超頻)找不出規則
1台1556(q9550 超頻)
1台1556(沒超頻)

筆電1台 195 左右
真是奇怪,ZwDelayExecution 改為ntDelayExecution
也是一樣
有高手能指點一二嗎?
謝謝



$timer=TimerInit()

for $i=1  to 100
    Hsleep(1)
next
MsgBox (0,1,TimerDiff($timer))

Func HSleep($iMicroSeconds,$hDll=False)
    Local $hStruct, $bLoaded
    If Not $hDll Then
        $hDll=DllOpen("ntdll.dll")
        $bLoaded=True
    EndIf
    $hStruct=DllStructCreate("int64 time;")
    DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10))
    DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct))
    If $bLoaded Then DllClose($hDll)
EndFunc
发表于 2011-1-2 14:55:55 | 显示全部楼层
同一台电脑上,运行100次,看看结果。
SLEEP本身就并不精确。
 楼主| 发表于 2011-1-2 15:50:04 | 显示全部楼层
不是用sleep,是call ntdll的 hsleep
跟精確無關
成功運行與無效
差距約15倍
发表于 2011-1-3 11:58:33 | 显示全部楼层
不清楚
发表于 2011-1-4 08:02:10 | 显示全部楼层
过来试试看....
发表于 2011-1-4 08:04:36 | 显示全部楼层
我这里99.2-99.3啊

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-1-4 15:40:26 | 显示全部楼层
这段代码的结果我电脑上在200~400之间不定。。。
发表于 2011-1-4 16:54:58 | 显示全部楼层
$file = FileOpen("test.txt", 1+2+8)

$i = 0

While $i < 100
test()
$i +=1;
WEnd
FileClose($file)


Func test()
$timer=TimerInit()       
for $i=1  to 100
    Hsleep(1)
next
$a = TimerDiff($timer)
FileWriteLine($file, $a & @CRLF)
EndFunc




;ClipPut(TimerDiff($timer))
Func HSleep($iMicroSeconds,$hDll=False)
    Local $hStruct, $bLoaded
    If Not $hDll Then
        $hDll=DllOpen("ntdll.dll")
        $bLoaded=True
    EndIf
    $hStruct=DllStructCreate("int64 time;")
    DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10))
    DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct))
    If $bLoaded Then DllClose($hDll)
EndFunc



1553.16889141251
1559.81560433849
1559.86603404968
1559.91607880124
1559.91145928571
1559.89991049689
1559.9268576708
1559.91299912422
1559.85871981676
1559.87642795962
1559.91107432608
1559.94687557142
1559.94302597515
1559.91107432608
1559.836777118
1559.85679501862
1559.918388559
1559.87681291924
1559.91299912422
1559.8775828385
1575.49308516269
1559.94572069254
1559.87873771738
1559.8972157795
1559.91492392235
1560.26754694101
1559.50648175773
1559.86102957452
1559.92762759006
1559.87912267701
1559.93879141925
1559.91954343788
1559.88412715216
1559.94918532919
1559.98383169565
1559.81521937887
1559.84563118943
1559.90376009316
1559.95765444099
1559.8044405093
1559.91684872049
1559.98383169565
1559.85102062421
1559.88643690993
1559.96381379503
1559.92647271117
1559.93378694409
1560.14243506212
1559.70396604656
1559.95572964285
1560.06428825777
1559.87103852483
1559.78249781054
1560.00461951553
1559.90722472981
1559.87180844409
1559.81752913663
1559.88528203105
1559.45759188505
1559.898755618
1560.07583704659
1559.76902422358
1560.02964189131
1559.89644586024
1559.92069831676
1559.93609670186
1559.91915847825
1560.05697402485
1559.83600719874
1559.88797674844
1559.9395613385
1559.90799464906
1559.82753808694
1559.93032230745
1559.86949868633
1560.14282002175
1559.52803949686
1559.88297227328
1560.05889882299
1559.78596244719
1560.74027736341
1559.16656240677
1559.95880931987
1559.72090427016
1559.99191584783
1560.01000895031
1559.80213075154
1559.98960609006
1559.92069831676
1559.92339303416
1559.89683081987
1560.08969559317
1559.82099377328
1559.92801254968
1559.86834380744
1559.93378694409
1559.96535363354
1561.46708113987
1558.43552407441
1559.91184424533
发表于 2011-1-4 16:57:09 | 显示全部楼层
$file = FileOpen("test.txt", 1+2+8)

$i = 0

While $i < 100
test()
$i +=1;
WEnd
FileClose($file)


Func test()
$timer=TimerInit()       
for $i=1  to 100
    Hsleep(1)
next
$a = TimerDiff($timer)
FileWriteLine($file, $a & @CRLF)
EndFunc




;ClipPut(TimerDiff($timer))
Func HSleep($iMicroSeconds,$hDll=False)
    Local $hStruct, $bLoaded
    If Not $hDll Then
        $hDll=DllOpen("ntdll.dll")
        $bLoaded=True
    EndIf
    $hStruct=DllStructCreate("int64 time;")
    DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10))
    DllCall($hDll,"dword","ntDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct))
    If $bLoaded Then DllClose($hDll)
EndFunc



3.21017833254737
2.8232939070515
2.82598862444301
2.8148247952496
3.00961436669329
3.02039323625935
2.82213902816942
2.86063499090533
2.89720615550444
2.81059023934865
2.98151231389608
2.79249713686277
2.78133330766936
2.77979346915992
2.78248818655144
2.77786867102313
2.86255978904212
2.78710770207974
2.77786867102313
2.81713455301375
2.7131954536268
2.77632883251369
2.78556786357031
2.71819992878247
2.66276574244276
2.74630198157968
2.66738525797107
3.12009777974535
2.69741210890508
2.97804767724985
2.68624827971166
2.68162876418336
2.70896089772585
2.68701819896638
2.67777916790977
2.68624827971166
2.681243804556
2.69471739151356
2.83946221140058
2.70434138219754
2.69125275486733
2.69125275486733
2.69279259337677
2.69009787598526
2.7620853263014
2.68932795673054
2.73513815238627
2.68855803747582
2.68817307784846
2.68701819896638
2.69471739151356
2.67315965238146
2.65622142877766
2.66392062132484
2.66584541946163
2.71011577660793
2.6708498946173
2.66738525797107
2.681243804556
2.66738525797107
2.66700029834371
2.84600652506568
2.69856698778716
2.68316860269279
2.68162876418336
2.681243804556
2.67970396604656
2.75246133561743
2.70742105921641
2.69279259337677
2.67508445051825
2.67585436977297
2.67854908716448
3.44885330151
2.69394747225885
2.70164666480603
2.68085884492864
2.68586332008431
2.75323125487215
2.76131540704669
2.75977556853725
2.7451471026976
2.75708085114574
2.70703609958906
2.75130645673535
2.7470719008344
2.67546941014561
2.67508445051825
2.67200477349938
2.86448458717892
2.7574658107731
2.6897129163579
2.68586332008431
2.68201372381072
2.68278364306543
2.7301336772306
2.67431453126353
2.66854013685315
2.69125275486733
2.67585436977297
发表于 2011-1-6 17:28:06 | 显示全部楼层
有意思 那个大大能解释是怎么回事啊
发表于 2011-1-7 03:19:31 | 显示全部楼层
用毫秒来计算微秒的精度,亏你想的出来
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-4 11:32 , Processed in 0.082134 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表