Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rubyを30倍速くした話

Tsukasa OISHI
February 21, 2018

 Rubyを30倍速くした話

Tsukasa OISHI

February 21, 2018
Tweet

More Decks by Tsukasa OISHI

Other Decks in Programming

Transcript

  1. QSFGT\UPLZP<   >^ BSFBT\FCJTV<  >^ HFOSFT\ZBLJOJLV< > DVSSZ<>^

    ౦ژɺܙൺणɺΧϨʔ͕ݕࡧ৚݅ͩͬͨ৔߹  QSFGT<UPLZP>BSFBT<FCJTV>  QSFGT<UPLZP>BSFBT<FCJTV>HFOSFT<DVSSZ>
  2. QSFGT\UPLZP<   >^ BSFBT\FCJTV<  >^ HFOSFT\ZBLJOJLV< > DVSSZ<>^

    ౦ژɺܙൺणɺΧϨʔ͕ݕࡧ৚݅ͩͬͨ৔߹  QSFGT<UPLZP>BSFBT<FCJTV>  QSFGT<UPLZP>BSFBT<FCJTV>HFOSFT<DVSSZ> ݅਺͕݅ະຬʹͳͬͨ࣌ʹɺͻͱͭલͷू߹Λ࢖͏
  3. 4FU

  4. QSFGT\UPLZP<   >^ BSFBT\FCJTV<  >^ HFOSFT\ZBLJOJLV< > DVSSZ<>^

    QSFGT\UPLZP^ BSFBT\FCJTV^ HFOSFT\ZBLJOJLV DVSSZ^
  5. void Init_figure_set(void) { rb_cFigureSet = rb_define_class("FigureSet", rb_cObject); rb_define_alloc_func(rb_cFigureSet, t_allocate); rb_define_private_method(rb_cFigureSet,

    "initialize", t_initialize, -1); rb_define_method(rb_cFigureSet, "initialize_copy", t_initialize_copy, 1); rb_define_method(rb_cFigureSet, "add", t_add, 1); rb_define_method(rb_cFigureSet, "delete", t_delete, 1); rb_define_method(rb_cFigureSet, "intersection", t_intersection, 1); ΠϯλʔϑΣʔε͸3VCZͩΑ
  6. require "figure_set" require "benchmark/ips" m3 = [] m5 = []

    (1..1_000_000).each do |i| m3 << i if i % 3 == 0 m5 << i if i % 5 == 0 end s_m3 = Set.new(m3) s_m5 = Set.new(m5) f_m3 = FigureSet.new(m3) f_m5 = FigureSet.new(m5) Benchmark.ips do |x| x.report("arry") { m3 & m5 } x.report("set") { s_m3 & s_m5 } x.report("figure_set") { f_m3 & f_m5 } x.compare! end