Skip to content

Instantly share code, notes, and snippets.

@nahi
nahi / fib_recursive_result.txt
Created November 27, 2011 16:01
Perf comparison of cast_off gem compiler and JRuby indy branch
% cat bench_fib_recursive.rb
require 'benchmark'
def fib_ruby(n)
if n < 2
n
else
fib_ruby(n - 2) + fib_ruby(n - 1)
end
end
forkjoin.rb: https://github.com/headius/forkjoin.rb
# examples/recursive/fibonacci.rb
% jruby -Ilib -v examples/recursive/fibonacci.rb 5 30
jruby 1.7.0.dev (ruby-1.8.7-p352) (2011-11-22 120223b) (Java HotSpot(TM) 64-Bit Server VM 1.7.0_02-ea) [linux-amd64-java]
fib(30) = 832040
1240.000010 [msec]
fib(30) = 832040
703.999996 [msec]
fib(30) = 832040
@nahi
nahi / G.java
Created November 16, 2011 12:45
import java.util.*;
public class G {
interface F {
int calc(int x);
}
public static void main(String[] args) {
F fib = (x) -> (x < 2) ? x : fib.calc(x - 1) + fib.calc(x - 2); // fibonacci
Comparator<String> reverse = (a, b) -> -1; // reverse sort
% ruby -ropenssl -e 'p OpenSSL::X509::Certificate.new(File.read("lib/mechanize/test_case.rb")).public_key.e'
1
% ruby -ropenssl -e 'puts OpenSSL::X509::Certificate.new(File.read("lib/mechanize/test_case.rb")).to_text'
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 0 (0x0)
Signature Algorithm: sha1WithRSAEncryption
Issuer: CN=nobody, DC=example
Validity
% uname -a
Linux ubuntu 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
% for a in {1..10}; do time JAVA_HOME=/home/nahi/java/jdk1.6.0_29 jruby165 -e 0; done
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.42s user 0.37s system 57% cpu 1.377 total
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.54s user 0.10s system 106% cpu 0.596 total
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.48s user 0.14s system 120% cpu 0.520 total
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.48s user 0.12s system 110% cpu 0.537 total
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.50s user 0.08s system 111% cpu 0.519 total
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.46s user 0.11s system 97% cpu 0.581 total
JAVA_HOME=/home/nahi/java/jdk1.6.0_29 /home/nahi/java/jruby-1.6.5/bin/jruby - 0.
require 'date'
class Date
# Try to read Marshal data of 1.9.3 Date
# !!CAUTION: COMPLETELY UNTESTED!!!
if RUBY_VERSION < "1.9.3"
def marshal_load(ary)
case ary.size
when 3
@ajd, @of, @sg, = ary
@nahi
nahi / gist:1325253
Created October 29, 2011 23:46
Long String in initial line causes VM aborting? (1000 vs 10000, existence of "\n" at initial line)
% jruby165 -e 'File.open("longstr.rb", "w") { |f| f << "str = %q(" << ("0" * 1000) << ")\np :sleep\n" }' && jruby165 longstr.rb
:sleep
% jruby165 -e 'File.open("longstr.rb", "w") { |f| f << "str = %q(" << ("0" * 10000) << ")\np :sleep\n" }' && jruby165 longstr.rb
% jruby165 -e 'File.open("longstr.rb", "w") { |f| f << "\n" << "str = %q(" << ("0" * 10000) << ")\np :sleep\n" }' && jruby165 longstr.rb
:sleep
%
require 'benchmark'
TIMES = 5000000
Benchmark.bmbm(40) do |x|
x.report('hoge[:fuga] = foo') do
hoge = {}
foo = :foo
TIMES.times do
hoge[:fuga] = foo
% ruby -e 'p(begin; raise; rescue => e; end)'
nil
% ruby187 -e 'p(begin; raise; rescue => e; end)'
RuntimeError
% jruby --1.9 -e 'p(begin; raise; rescue => e; end)'
RuntimeError
% jruby --1.8 -e 'p(begin; raise; rescue => e; end)'
RuntimeError
% jruby -J-Xbootclasspath/a:$JRUBY_HOME/build_lib/jgrapht-jdk1.5.jar -X-CIR -e 'p(begin; raise; rescue => e; end)'
RuntimeError
require 'zlib'
class MyInflate
def initialize(input)
@z = Zlib::Inflate.new
@input = input
@buf = ''
end
def read(n)