Skip to content

Instantly share code, notes, and snippets.

@curi1119
Last active August 29, 2015 14:08
Show Gist options
  • Save curi1119/1fc84e6702073751829a to your computer and use it in GitHub Desktop.
Save curi1119/1fc84e6702073751829a to your computer and use it in GitHub Desktop.
どれぐらい正確に時を刻むか
----1------------------------
1414464532.262281
1414464532.414477
1414464532.565821
1414464532.718029
1414464532.870191
1414464533.021643
1414464533.173451
1414464533.325511
1414464533.476955
1414464533.628726
1414464533.780651
1414464533.932081
1414464534.083474
1414464534.235432
1414464534.387653
1414464534.538932
1414464534.6908228
1414464534.842419
1414464534.9940832
1414464535.1462169
----2------------------------
1414464536.399381
1414464536.55086
1414464536.702819
1414464536.854849
1414464537.006619
1414464537.1578188
1414464537.3100312
1414464537.461636
1414464537.613836
1414464537.765484
1414464537.91664
1414464538.06862
1414464538.220821
1414464538.3730361
1414464538.5252109
1414464538.676152
1414464538.827395
1414464538.9789498
1414464539.1308641
1414464539.283115
----3------------------------
1414464540.436115
1414464540.5359402
1414464540.635743
1414464540.735862
1414464540.835504
1414464540.935219
1414464541.03617
1414464541.1368299
1414464541.237046
1414464541.337528
1414464541.4377332
1414464541.537689
1414464541.6382132
1414464541.7382982
1414464541.838428
1414464541.93826
1414464542.038154
1414464542.137918
1414464542.23811
1414464542.338052
----4------------------------
1414464543.540579
1414464543.641703
1414464543.742647
1414464543.843785
1414464543.944261
1414464544.045388
1414464544.146312
1414464544.2474618
1414464544.348491
1414464544.449621
1414464544.550539
1414464544.651604
1414464544.752359
1414464544.853483
1414464544.954577
1414464545.054802
1414464545.154973
1414464545.256095
1414464545.356895
1414464545.4575539
----1 result-----------------------
diff: 0.15219616889953613
diff: 0.15134382247924805
diff: 0.1522080898284912
diff: 0.1521620750427246
diff: 0.15145182609558105
diff: 0.1518080234527588
diff: 0.15206003189086914
diff: 0.1514439582824707
diff: 0.15177106857299805
diff: 0.15192508697509766
diff: 0.1514298915863037
diff: 0.15139293670654297
diff: 0.15195798873901367
diff: 0.1522212028503418
diff: 0.15127897262573242
diff: 0.15189075469970703
diff: 0.1515960693359375
diff: 0.15166425704956055
diff: 0.15213370323181152
avg:0.15178610149182772
----2 result-----------------------
diff: 0.15147900581359863
diff: 0.15195918083190918
diff: 0.15202999114990234
diff: 0.15176987648010254
diff: 0.1511998176574707
diff: 0.15221238136291504
diff: 0.15160489082336426
diff: 0.15219998359680176
diff: 0.15164804458618164
diff: 0.15115594863891602
diff: 0.15197992324829102
diff: 0.15220093727111816
diff: 0.15221524238586426
diff: 0.152174711227417
diff: 0.1509411334991455
diff: 0.15124297142028809
diff: 0.15155482292175293
diff: 0.15191435813903809
diff: 0.1522507667541504
avg:0.1517754730425383
----3 result-----------------------
diff: 0.09982514381408691
diff: 0.09980273246765137
diff: 0.10011911392211914
diff: 0.09964203834533691
diff: 0.09971499443054199
diff: 0.10095095634460449
diff: 0.10065984725952148
diff: 0.10021615028381348
diff: 0.10048198699951172
diff: 0.1002051830291748
diff: 0.09995579719543457
diff: 0.1005241870880127
diff: 0.10008502006530762
diff: 0.10012984275817871
diff: 0.09983205795288086
diff: 0.09989380836486816
diff: 0.09976410865783691
diff: 0.10019207000732422
diff: 0.09994196891784668
avg:0.10010194778442383
----4 result-----------------------
diff: 0.10112380981445312
diff: 0.10094404220581055
diff: 0.10113811492919922
diff: 0.10047602653503418
diff: 0.10112690925598145
diff: 0.10092401504516602
diff: 0.1011497974395752
diff: 0.1010291576385498
diff: 0.10113000869750977
diff: 0.10091805458068848
diff: 0.10106492042541504
diff: 0.10075497627258301
diff: 0.10112404823303223
diff: 0.10109400749206543
diff: 0.10022497177124023
diff: 0.10017108917236328
diff: 0.10112190246582031
diff: 0.1008000373840332
diff: 0.10065889358520508
avg:0.10089340962861713
# -*- coding: utf-8 -*-
require 'eventmachine'
require 'em-synchrony'
@r1,@r2,@r3,@r4 = [],[],[],[]
def case1
@counter += 1
EM.stop_event_loop if @counter >= 20
@r1 << Time.now.to_f
puts @r1.last
sleep 0.05
EM::Synchrony.add_timer(0.1) { case1 }
end
def case2
@counter += 1
EM.stop_event_loop if @counter >= 20
@r2 << Time.now.to_f
puts @r2.last
sleep 0.05
end
def case3
@counter += 1
EM.stop_event_loop if @counter >= 20
t0 = Time.now
@r3 << Time.now.to_f
puts @r3.last
sleep 0.05
EM::Synchrony.add_timer(0.1 - (Time.now - t0).to_f) { case3 }
end
def case4
@counter += 1
EM.stop_event_loop if @counter >= 20
@r4 << Time.now.to_f
puts @r4.last
sleep 0.05
end
puts "----1------------------------"
@counter = 0
EM.synchrony {
case1
}
sleep 1
puts "----2------------------------"
@counter = 0
EM.synchrony {
EM::Synchrony.add_periodic_timer(0.1) { case2 }
}
sleep 1
puts "----3------------------------"
@counter = 0
EM.synchrony {
case3
}
sleep 1
puts "----4------------------------"
@counter = 0
EM.synchrony {
EM::Synchrony.add_periodic_timer(0.1) { EM::Synchrony.next_tick { case4 } }
}
def output(arr)
diffs = []
last_t = nil
arr.each do |t|
if last_t.nil?
last_t = t
next
end
diff = t - last_t
puts "diff: #{diff}"
diffs << diff
last_t = t
end
puts "avg:#{diffs.inject(0.0){|sum,i| sum+=i } / diffs.size}"
end
puts "----1 result-----------------------"
output(@r1)
puts "----2 result-----------------------"
output(@r2)
puts "----3 result-----------------------"
output(@r3)
puts "----4 result-----------------------"
output(@r4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment