-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.cr
2205 lines (1850 loc) · 45.9 KB
/
metric.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "digest/crc32"
require "big"
require "base64"
require "json"
require "complex"
# ./metric
# ./metric Brainfuck2
# ./metric Brainfuck,Brainfuck2
Benchmark.run(ARGV[0]?)
abstract class Benchmark
abstract def run
abstract def result
abstract def expected
FILENAME = "#{__DIR__}/results.js"
def self.release_results_error
puts "ERROR\nNot found release results!\nBefore run in any other mode, you should compile and run this metric in --release mode,\nbecause all other modes compare to release."
exit 1
end
def self.load_release_results
release_results = {} of String => Float64
{% unless flag?(:release) %}
if File.exists?(FILENAME)
release_results = Hash(String, Float64).from_json(File.read(FILENAME))
{% for kl in @type.subclasses %}
release_results_error unless release_results["{{kl.id}}"]?
{% end %}
else
release_results_error
end
{% end %}
release_results
end
def self.run(filter : String? = nil)
release_results = load_release_results
results = {} of String => Float64
summary_time = 0.0
awards = 0.0
ok = 0
fails = 0
silent = ENV["SILENT"]? == "1"
run_mark = ENV["RUN_MARK"]? == "1"
if filter
filter = filter.split(",").map(&.strip)
end
{% for kl in @type.subclasses %}
if !filter || (filter && filter.includes?("{{kl.id}}"))
print "{{kl}}: " unless silent
puts "---RUN---" if run_mark
bench = {{kl.id}}.new
bench._run
results["{{kl.id}}"] = bench.time_delta
{% if flag?(:release) %}
award = 1.0
{% else %}
award = release_results["{{kl.id}}"] / bench.time_delta
{% end %}
if bench.ok?
print "ok " unless silent
ok += 1
else
print "err result=#{bench.result.inspect}, but expected=#{bench.expected.inspect} " unless silent
fails += 1
end
unless silent
print "in %.3fs, award %.1f\n" % {bench.time_delta, award}
end
summary_time += bench.time_delta
awards += award
end
{% end %}
{% if flag?(:release) %}
if !filter
File.open(FILENAME, "w") { |f| results.to_json(f) }
end
{% end %}
if ok + fails > 0
puts "%.4fs, %.2f/%d, %.2f" % {summary_time, awards, ok + fails, awards / (ok + fails).to_f}
end
exit 1 if fails > 0
end
getter time_delta : Float64 = 0
def _run
GC.collect
t = Time.local
self.run
@time_delta = (Time.local - t).to_f
GC.collect
sleep 0 # context switch, may be needed or maybe not, to close some coroutines
GC.collect
end
def ok?
self.result == self.expected
end
end
class Pidigits < Benchmark
def initialize(@nn = 4500)
@result = IO::Memory.new
end
def run
start = Time.local
i = 0
k = 0
ns = 0.to_big_i
a = 0.to_big_i
t = 0
u = 0.to_big_i
k1 = 1
n = 1.to_big_i
d = 1.to_big_i
while true
k += 1
t = n << 1
n *= k
k1 += 2
a = (a + t) * k1
d *= k1
if a >= n
t, u = (n * 3 + a).divmod(d)
u += n
if d > u
ns = ns * 10 + t
i += 1
if i % 10 == 0
@result << "%010d\t:%d\n" % {ns.to_u64, i}
ns = 0
end
break if i >= @nn
a = (a - (d * t)) * 10
n *= 10
end
end
end
end
def result
Digest::CRC32.checksum(@result.to_s)
end
def expected
2259604824
end
end
class Binarytrees < Benchmark
getter result
class TreeNode
property left : TreeNode?
property right : TreeNode?
property item : Int32
def self.create(item, depth) : TreeNode
TreeNode.new item, depth - 1
end
def initialize(@item, depth = 0)
if depth > 0
self.left = TreeNode.new 2 * item - 1, depth - 1
self.right = TreeNode.new 2 * item, depth - 1
end
end
def check
return item if (lft = left).nil?
return item if (rgt = right).nil?
lft.check - rgt.check + item
end
end
def initialize(@n = 18)
@result = 0
end
def run
min_depth = 4
max_depth = Math.max min_depth + 2, @n
stretch_depth = max_depth + 1
@result += TreeNode.create(0, stretch_depth).check
long_lived_tree = TreeNode.create 0, max_depth
min_depth.step(to: max_depth, by: 2) do |depth|
iterations = 1 << (max_depth - depth + min_depth)
1.upto(iterations) do |i|
@result += TreeNode.create(i, depth).check
@result += TreeNode.create(-i, depth).check
end
end
end
def expected
-699041
end
end
class Brainfuck < Benchmark
getter result
class Tape
def initialize
@tape = [0]
@pos = 0
end
@[AlwaysInline]
def get
@tape[@pos]
end
@[AlwaysInline]
def inc
@tape[@pos] += 1
end
@[AlwaysInline]
def dec
@tape[@pos] -= 1
end
@[AlwaysInline]
def advance
@pos += 1
@tape << 0 if @tape.size <= @pos
end
@[AlwaysInline]
def devance
@pos -= 1 if @pos > 0
end
end
class Program
def initialize(text)
@chars = [] of Char
@bracket_map = {} of Int32 => Int32
leftstack = [] of Int32
pc = 0
text.each_char do |char|
if "[]<>+-,.".includes?(char)
@chars << char
if char == '['
leftstack << pc
elsif char == ']' && !leftstack.empty?
left = leftstack.pop
right = pc
@bracket_map[left] = right
@bracket_map[right] = left
end
pc += 1
end
end
end
def run
result = 0
tape = Tape.new
pc = 0
while pc < @chars.size
case @chars[pc]
when '+'; tape.inc
when '-'; tape.dec
when '>'; tape.advance
when '<'; tape.devance
when '['; pc = @bracket_map[pc] if tape.get == 0
when ']'; pc = @bracket_map[pc] if tape.get != 0
when '.'; result += tape.get.chr.ord
else
end
pc += 1
end
result
end
end
def initialize
@text = "Benchmark brainf*ck program
>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."
@result = 0
end
def run
@result = Program.new(@text).run
end
def expected
2025
end
end
class Brainfuck2 < Benchmark
getter result
module Op
record Inc, val : Int32
record Move, val : Int32
record Print
alias T = Inc | Move | Print | Array(Op::T)
end
class Tape
def initialize
@tape = [0_u8]
@pos = 0
end
@[AlwaysInline]
def get
@tape[@pos]
end
@[AlwaysInline]
def inc(x)
@tape[@pos] += x
end
@[AlwaysInline]
def move(x)
@pos += x
while @pos >= @tape.size
@tape << 0
end
end
end
class Program
property result
@ops : Array(Op::T)
def initialize(code : String)
@ops = parse(code.each_char)
@result = 0
end
def run
_run @ops, Tape.new
end
private def _run(program, tape)
program.each do |op|
case op
when Op::Inc
tape.inc(op.val)
when Op::Move
tape.move(op.val)
when Array(Op::T)
while tape.get != 0
_run(op, tape)
end
when Op::Print
@result += tape.get.chr.ord
else
end
end
end
private def parse(iterator)
res = [] of Op::T
iterator.each do |c|
op = case c
when '+'; Op::Inc.new(1)
when '-'; Op::Inc.new(-1)
when '>'; Op::Move.new(1)
when '<'; Op::Move.new(-1)
when '.'; Op::Print.new
when '['; parse(iterator)
when ']'; break
else
end
res << op if op
end
res
end
end
def initialize
@text = "Benchmark brainf*ck program
>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++."
@result = 0
end
def run
program = Program.new(@text)
program.run
@result = program.result
end
def expected
2025
end
end
class Fannkuchredux < Benchmark
getter result
def fannkuchredux(n)
perm1 = StaticArray(Int32, 32).new { |i| i }
perm = StaticArray(Int32, 32).new(0)
count = StaticArray(Int32, 32).new(0)
maxFlipsCount = permCount = checksum = 0
r = n
while true
while r > 1
count[r - 1] = r
r -= 1
end
n.times { |i| perm[i] = perm1[i] } # dup
flipsCount = 0
while !((k = perm[0]) == 0)
k2 = (k + 1) >> 1
(0...k2).each do |i|
j = k - i
perm[i], perm[j] = perm[j], perm[i] # swap
end
flipsCount += 1
end
maxFlipsCount = flipsCount if flipsCount > maxFlipsCount
checksum += (permCount % 2 == 0) ? flipsCount : -flipsCount
while true
return {checksum, maxFlipsCount} if r == n
perm0 = perm1[0]
(0...r).each do |i|
j = i + 1
perm1[i], perm1[j] = perm1[j], perm1[i] # swap
end
perm1[r] = perm0
cntr = count[r] -= 1
break if cntr > 0
r += 1
end
permCount += 1
end
end
def initialize(@n = 11)
@result = {0, 0}
end
def run
@result = fannkuchredux(@n)
end
def expected
{556355, 51}
end
end
class Fasta < Benchmark
IM = 139968
IA = 3877
IC = 29573
class Last
@[AlwaysInline]
def self.last
@@last ||= 42
end
@[AlwaysInline]
def self.last=(x)
@@last = x
end
end
def gen_random(max)
Last.last = (Last.last * IA + IC) % IM
max * Last.last / IM.to_f64
end
def make_cumulative(genelist)
cp = 0.0_f64
genelist.size.times do |i|
c, p = genelist[i]
cp += p
genelist[i] = {c, cp}
end
end
def select_random(genelist)
r = gen_random(1)
return genelist[0][0] if r < genelist[0][1]
lo = 0
hi = genelist.size - 1
while hi > lo + 1
i = (hi + lo) // 2
if r < genelist[i][1]
hi = i
else
lo = i
end
end
genelist[hi][0]
end
LINE_LENGTH = 60
def make_random_fasta(id, desc, genelist, n)
todo = n
@result << ">#{id} #{desc}"
@result << "\n"
while todo > 0
m = (todo < LINE_LENGTH) ? todo : LINE_LENGTH
pick = String.new(m) do |buffer|
m.times { |i| buffer[i] = select_random(genelist).ord.to_u8 }
{m, m}
end
@result << pick
@result << "\n"
todo -= LINE_LENGTH
end
end
def make_repeat_fasta(id, desc, s, n)
todo = n
k = 0
kn = s.size
@result << ">#{id} #{desc}"
@result << "\n"
while todo > 0
m = (todo < LINE_LENGTH) ? todo : LINE_LENGTH
while m >= kn - k
@result << s[k..-1]
m -= kn - k
k = 0
end
@result << s[k...k + m]
@result << "\n"
k += m
todo -= LINE_LENGTH
end
end
IUB = [
{'a', 0.27},
{'c', 0.12},
{'g', 0.12},
{'t', 0.27},
{'B', 0.02},
{'D', 0.02},
{'H', 0.02},
{'K', 0.02},
{'M', 0.02},
{'N', 0.02},
{'R', 0.02},
{'S', 0.02},
{'V', 0.02},
{'W', 0.02},
{'Y', 0.02},
]
HOMO = [{'a', 0.3029549426680}, {'c', 0.1979883004921}, {'g', 0.1975473066391}, {'t', 0.3015094502008}]
ALU = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"
def initialize(@n = 7000000)
make_cumulative(IUB)
make_cumulative(HOMO)
@result = IO::Memory.new
end
def run
make_repeat_fasta("ONE", "Homo sapiens alu", ALU, @n * 2)
make_random_fasta("TWO", "IUB ambiguity codes", IUB, @n * 3)
make_random_fasta("THREE", "Homo sapiens frequency", HOMO, @n * 5)
end
def result
Digest::CRC32.checksum(@result.to_s)
end
def expected
223599119
end
end
class Knuckeotide < Benchmark
@seq : String
def frecuency(seq, length)
n = seq.size - length + 1
table = Hash(String, Int32).new { 0 }
(0...n).each do |f|
table[seq.byte_slice(f, length)] += 1
end
{n, table}
end
def sort_by_freq(seq, length)
n, table = frecuency(seq, length)
table.to_a.sort { |a, b| b[1] <=> a[1] }.each do |v|
@result << "%s %.3f\n" % {v[0].upcase, ((v[1] * 100).to_f / n)}
end
@result << "\n"
end
def find_seq(seq, s)
n, table = frecuency(seq, s.size)
@result << "#{table[s].to_s}\t#{s.upcase}\n"
end
def run(seq)
end
def initialize
@result = IO::Memory.new
f = Fasta.new(600000)
f.run
res = [email protected]_s
seq = IO::Memory.new
three = false
res.each_line do |line|
if line.starts_with?(">THREE")
three = true
next
end
seq << line.chomp if three
end
@seq = seq.to_s
end
def run
(1..2).each { |i| sort_by_freq(@seq, i) }
%w(ggt ggta ggtatt ggtattttaatt ggtattttaatttatagt).each { |s| find_seq(@seq, s) }
end
def result
Digest::CRC32.checksum(@result.to_s)
end
def expected
159474310
end
end
class Mandelbrot < Benchmark
ITER = 50
LIMIT = 2.0
def initialize(@n = 3800)
@result = IO::Memory.new
end
def run
w = h = @n
@result << "P4\n#{w} #{h}\n"
bit_num = 0
byte_acc = 0_u8
h.times do |y|
w.times do |x|
zr = zi = tr = ti = 0.0
cr = (2.0 * x / w - 1.5)
ci = (2.0 * y / h - 1.0)
i = 0
while (i < ITER) && (tr + ti <= LIMIT * LIMIT)
zi = 2.0 * zr * zi + ci
zr = tr - ti + cr
tr = zr * zr
ti = zi * zi
i += 1
end
byte_acc <<= 1
byte_acc |= 0x01 if tr + ti <= LIMIT * LIMIT
bit_num += 1
if bit_num == 8
@result.write_byte byte_acc
byte_acc = 0_u8
bit_num = 0
elsif x == w - 1
byte_acc <<= 8 - w % 8
@result.write_byte byte_acc
byte_acc = 0_u8
bit_num = 0
end
end
end
end
def result
Digest::CRC32.checksum(@result.to_s)
end
def expected
2862550248
end
end
class Matmul < Benchmark
def matmul(a, b)
m = a.size
n = a[0].size
p = b[0].size
# transpose
b2 = Array.new(n) { Array.new(p, 0.0) }
(0...n).each do |i|
(0...p).each do |j|
b2[j][i] = b[i][j]
end
end
# multiplication
c = Array.new(m) { Array.new(p, 0.0) }
c.each_with_index do |ci, i|
ai = a[i]
b2.each_with_index do |b2j, j|
s = 0.0
b2j.each_with_index do |b2jv, k|
s += ai[k] * b2jv
end
ci[j] = s
end
end
c
end
def matgen(n)
tmp = 1.0 / n / n
a = Array.new(n) { Array.new(n, 0.0) }
(0...n).each do |i|
(0...n).each do |j|
a[i][j] = tmp * (i - j) * (i + j)
end
end
a
end
getter result
def initialize(@n = 980)
@result = 0.0
end
def run
a = matgen(@n)
b = matgen(@n)
c = matmul(a, b)
@result = c[@n >> 1][@n >> 1]
end
def expected
-93.66692176867205
end
end
class Nbody < Benchmark
# Copied with little modifications from: http://benchmarksgame.alioth.debian.org/u32/program.php?test=nbody&lang=yarv&id=2
SOLAR_MASS = 4 * Math::PI**2
DAYS_PER_YEAR = 365.24
class Planet
def_clone
property x : Float64
property y : Float64
property z : Float64
property vx : Float64
property vy : Float64
property vz : Float64
property mass : Float64
def initialize(@x, @y, @z, vx, vy, vz, mass)
@vx, @vy, @vz = vx * DAYS_PER_YEAR, vy * DAYS_PER_YEAR, vz * DAYS_PER_YEAR
@mass = mass * SOLAR_MASS
end
def move_from_i(bodies, nbodies, dt, i)
while i < nbodies
b2 = bodies[i]
dx = @x - b2.x
dy = @y - b2.y
dz = @z - b2.z
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
mag = dt / (distance * distance * distance)
b_mass_mag, b2_mass_mag = @mass * mag, b2.mass * mag
@vx -= dx * b2_mass_mag
@vy -= dy * b2_mass_mag
@vz -= dz * b2_mass_mag
b2.vx += dx * b_mass_mag
b2.vy += dy * b_mass_mag
b2.vz += dz * b_mass_mag
i += 1
end
@x += dt * @vx
@y += dt * @vy
@z += dt * @vz
end
end
def energy(bodies)
e = 0.0
nbodies = bodies.size
0.upto(nbodies - 1) do |i|
b = bodies[i]
e += 0.5 * b.mass * (b.vx * b.vx + b.vy * b.vy + b.vz * b.vz)
(i + 1).upto(nbodies - 1) do |j|
b2 = bodies[j]
dx = b.x - b2.x
dy = b.y - b2.y
dz = b.z - b2.z
distance = Math.sqrt(dx * dx + dy * dy + dz * dz)
e -= (b.mass * b2.mass) / distance
end
end
e
end
def offset_momentum(bodies)
px, py, pz = 0.0, 0.0, 0.0
bodies.each do |b|
m = b.mass
px += b.vx * m
py += b.vy * m
pz += b.vz * m
end
b = bodies[0]
b.vx = -px / SOLAR_MASS
b.vy = -py / SOLAR_MASS
b.vz = -pz / SOLAR_MASS
end
BODIES = [
# sun
Planet.new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
# jupiter
Planet.new(
4.84143144246472090e+00,
-1.16032004402742839e+00,
-1.03622044471123109e-01,
1.66007664274403694e-03,
7.69901118419740425e-03,
-6.90460016972063023e-05,
9.54791938424326609e-04),
# saturn
Planet.new(
8.34336671824457987e+00,
4.12479856412430479e+00,
-4.03523417114321381e-01,
-2.76742510726862411e-03,
4.99852801234917238e-03,
2.30417297573763929e-05,
2.85885980666130812e-04),
# uranus
Planet.new(
1.28943695621391310e+01,
-1.51111514016986312e+01,
-2.23307578892655734e-01,
2.96460137564761618e-03,
2.37847173959480950e-03,
-2.96589568540237556e-05,
4.36624404335156298e-05),
# neptune
Planet.new(
1.53796971148509165e+01,
-2.59193146099879641e+01,
1.79258772950371181e-01,
2.68067772490389322e-03,
1.62824170038242295e-03,
-9.51592254519715870e-05,
5.15138902046611451e-05),
]
def initialize(@n = 15000000)
@result = {0.0, 0.0}
@body = BODIES
end
def run
offset_momentum(@body)
v1 = energy(@body)
nbodies = @body.size
dt = 0.01
@n.times do
i = 0
while i < nbodies
b = @body[i]
b.move_from_i(@body, nbodies, dt, i + 1)
i += 1
end
end
v2 = energy(@body)
@result = {v1, v2}
end
getter result
def expected
{-0.16907516382852447, -0.16905678767532126}
end
end
class RegexDna < Benchmark
@seq : String
@ilen : Int32
@clen : Int32
def initialize
@result = IO::Memory.new
f = Fasta.new(6000000)
f.run
res = [email protected]_s
seq = IO::Memory.new
@ilen = 0
res.each_line do |line|
@ilen += line.bytesize + 1
seq << line.chomp unless line.starts_with? '>'
end
@seq = seq.to_s
@clen = seq.bytesize
end
def run
[
/agggtaaa|tttaccct/,
/[cgt]gggtaaa|tttaccc[acg]/,
/a[act]ggtaaa|tttacc[agt]t/,
/ag[act]gtaaa|tttac[agt]ct/,
/agg[act]taaa|ttta[agt]cct/,
/aggg[acg]aaa|ttt[cgt]ccct/,
/agggt[cgt]aa|tt[acg]accct/,
/agggta[cgt]a|t[acg]taccct/,
/agggtaa[cgt]|[acg]ttaccct/,
].each { |f| @result << "#{f.source} #{@seq.scan(f).size}\n" }
hash = {
"B" => "(c|g|t)",
"D" => "(a|g|t)",
"H" => "(a|c|t)",
"K" => "(g|t)",
"M" => "(a|c)",
"N" => "(a|c|g|t)",
"R" => "(a|g)",
"S" => "(c|t)",
"V" => "(a|c|g)",
"W" => "(a|t)",
"Y" => "(c|t)",