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

Ruby Racing: Challenging Ruby Methods

Ruby Racing: Challenging Ruby Methods

Avatar for Danielle Adams

Danielle Adams

November 25, 2016

Other Decks in Technology

Transcript

  1. static VALUE rb_ary_pop_m(int argc, VALUE *argv, VALUE ary) { VALUE

    result; if (argc == 0) { return rb_ary_pop(ary); } rb_ary_modify(ary); result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST); ARY_INCREASE_LEN(ary, -RARRAY_LEN(result)); return str; }
  2. #flatten_race.rb require 'ruby_racer' require_relative 'flatten' input = [ 1, [

    2, 3 ] ] proc = Proc.new { |input| flatten(input) }
  3. #flatten_race.rb require 'ruby_racer' require_relative 'flatten' input = [ 1, [

    2, 3 ] ] proc = Proc.new { |input| flatten(input) } RubyRacer.go(:flatten, input, &proc)
  4. # max with while loop def max(array) (...) while i

    < arr.length if array[i] > max max = array[i] (...) end
  5. # benchmark iterations = 1000000 input = [ 1, 2,

    3 ] operation = el + el user total real with each: 0.2500 0.2500 (0.251002) with while: 0.2200 0.2200 (0.226902)
  6. # benchmark iterations = 1000000 user total real with append:

    0.1000 0.1000 (0.096824) with assign: 0.0900 0.0900 (0.095280)
  7. static VALUE rb_ary_flatten(int argc, VALUE *argv, VALUE ary) { int

    mod = 0, level = -1; VALUE result, lv; (...) result = flatten(ary, level, &mod); OBJ_INFECT(result, ary); return result; } # ruby/array.c:4631
  8. static VALUE rb_ary_flatten(int argc, VALUE *argv, VALUE ary) { int

    mod = 0, level = -1; VALUE result, lv; (...) result = flatten(ary, level, &mod); OBJ_INFECT(result, ary); return result; } # ruby/array.c:4641
  9. static VALUE flatten(VALUE ary, int level, int *modified) { long

    i = 0; VALUE stack, result, tmp, elt; (...) stack = ary_new(0, ARY_DEFAULT_SIZE); result = ary_new(0, RARRAY_LEN(ary)); # ruby/array.c:4508
  10. static VALUE flatten(VALUE ary, int level, int *modified) { long

    i = 0; VALUE stack, result, tmp, elt; (...) stack = ary_new(0, ARY_DEFAULT_SIZE); result = ary_new(0, RARRAY_LEN(ary)); # ruby/array.c:4516
  11. static VALUE flatten(VALUE ary, int level, int *modified) { long

    i = 0; VALUE stack, result, tmp, elt; (...) stack = ary_new(0, ARY_DEFAULT_SIZE); result = ary_new(0, RARRAY_LEN(ary)); # ruby/array.c:4517
  12. static VALUE flatten(VALUE ary, int level, int *modified) { (...)

    while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4508
  13. static VALUE flatten(VALUE ary, int level, int *modified) { (...)

    while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4544
  14. static VALUE flatten(VALUE ary, int level, int *modified) { (...)

    while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4525
  15. static VALUE flatten(VALUE ary, int level, int *modified) { (...)

    while (i < RARRAY_LEN(ary)) { if (level >= 0 && RARRAY_LEN(stack) / 2 >= level) { rb_ary_push(result, elt); continue; (...) else { rb_ary_push(stack, ary); # ruby/array.c:4526
  16. static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if

    (RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4382
  17. static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if

    (RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4387
  18. static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if

    (RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4388
  19. static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if

    (RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4394
  20. static VALUE rb_ary_uniq(VALUE ary) { VALUE hash, uniq; (...) if

    (RARRAY_LEN(ary) <= 1) return rb_ary_dup(ary); (...) else { hash = ary_make_hash(ary); uniq = rb_hash_values(hash); } # ruby/array.c:4395
  21. There are a few factors that will affect performance if

    we are willing to sacrifice other methodologies.
  22. I didn't work hard to make Ruby perfect for everyone,

    because you feel differently from me. No language is perfect for everyone. [...] The perfect language for Guido van Rossum is probably Python. - Yukihiro "Matz" Matsumoto
  23. If you want your code to run faster, you should

    probably just use PyPy. - Guido van Rossum