z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
class Integer
def sqrt_prime?
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? }
end
end
(2..100_000).each &:sqrt_prime? #=> 1.1 s
class Integer
def skipping_prime?
return true if self == 2
return false if self.even?
3.step(Math.sqrt(self).floor, 2).all? { |i| (self % i).nonzero? }
end
end
(2..100_000).each &:skipping_prime? #=> 0.6 s
require 'inline'
class Integer
inline do |builder|
builder.include '<math.h>'
builder.c '
VALUE inline_prime() {
long i, number = NUM2LONG(self);
if (number == 2) return Qtrue;
if (number % 2 == 0) return Qfalse;
for (i = 3; i <= floor(sqrt(number)); i = i + 2) {
if (number % i == 0) return Qfalse;
}
return Qtrue;
}
'
end
end
require 'prime'
(2..100_000).each &:prime?
require 'benchmark'
require 'inline'
require 'prime'
class Integer
# …
end
range = 2..100_000
Benchmark.bmbm do |bench|
bench.report('sqrt:') { range.each &:sqrt_prime? }
bench.report('skipping:') { range.each &:skipping_prime? }
bench.report('inline:') { range.each &:inline_prime }
bench.report('stdlib:') { range.each &:prime? }
end
$ ruby bench-prime.rb
Rehearsal ---------------------------------------------
sqrt: 1.150000 0.000000 1.150000 ( 1.149098)
skipping: 0.640000 0.000000 0.640000 ( 0.645695)
inline: 0.050000 0.000000 0.050000 ( 0.048040)
stdlib: 1.850000 0.000000 1.850000 ( 1.854002)
------------------------------------ total: 3.690000sec
user system total real
sqrt: 1.140000 0.000000 1.140000 ( 1.135499)
skipping: 0.640000 0.000000 0.640000 ( 0.642145)
inline: 0.050000 0.000000 0.050000 ( 0.047625)
stdlib: 1.890000 0.000000 1.890000 ( 1.885808)
class Integer
def popcount_to_s
to_s(2).count '1'
end
end
require 'inline'
class Integer
inline do |builder|
builder.c '
int popcount_builtin() {
return __builtin_popcountl(NUM2LONG(self));
}
'
end
end
require 'benchmark'
require 'inline'
class Integer
# …
end
range = 0...2**22
Benchmark.bmbm do |bench|
bench.report('to_s:') { range.each &:popcount_to_s }
bench.report('builtin:') { range.each &:popcount_builtin }
end
$ ruby bench-popcount.rb
Rehearsal --------------------------------------------
to_s: 5.870000 0.000000 5.870000 ( 5.886263)
builtin: 0.520000 0.000000 0.520000 ( 0.515771)
----------------------------------- total: 6.390000sec
user system total real
to_s: 5.860000 0.010000 5.870000 ( 5.883422)
builtin: 0.510000 0.010000 0.520000 ( 0.520176)
#include "nf2util.h"
unsigned get_register(unsigned reg) {
struct nf2device nf2;
unsigned val;
nf2.device_name = "nf2c0";
check_iface(&nf2);
openDescriptor(&nf2);
readReg(&nf2, reg, &val);
closeDescriptor(&nf2);
return val;
}
#include "nf2util.h"
void set_register(unsigned reg, unsigned val) {
struct nf2device nf2;
nf2.device_name = "nf2c0";
check_iface(&nf2);
openDescriptor(&nf2);
writeReg(&nf2, reg, val);
closeDescriptor(&nf2);
}
require 'ffi'
class NetFPGA::Bridge
extend FFI::Library
ffi_lib "regset.so"
attach_function :get_register, [:uint], :uint
attach_function :set_register, [:uint, :uint], :void
end
bridge = NetFPGA::Bridge.new
bridge.get_register 0x2000200 #=> 0xbaadf00d
bridge.set_register 0x2000204, 0xdeadbeef #=> nil