Cより速いRubyプログラム

7,876 views
7,650 views

Published on

RubyKaigi2007 Presentation

Published in: Technology
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,876
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
0
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

Cより速いRubyプログラム

  1. 1. RubyKaigi 2007 SessionC Ruby An Example of Ruby Program Faster Than C makoto kuwata http://www.kuwata-lab.com copyright(c) 2007 kuwata-lab.com all rights reserved.
  2. 2. …copyright(c) 2007 kuwata-lab.com all rights reserved.
  3. 3. 2007 Script Languages in 2007 copyright(c) 2007 kuwata-lab.com all rights reserved.
  4. 4. PS3 Break Away from PS3 Syndrome‣ PS3/PSP … •‣ Wii/DS … • copyright(c) 2007 kuwata-lab.com all rights reserved.
  5. 5. Script Languages cant be Center Player‣ Java • Java • ≠‣ • • copyright(c) 2007 kuwata-lab.com all rights reserved.
  6. 6. Problems and Truth about Script Languages✓✓✓ copyright(c) 2007 kuwata-lab.com all rights reserved.
  7. 7. About This Presentationcopyright(c) 2007 kuwata-lab.com all rights reserved.
  8. 8. Point ≠Language Speed ≠ Application Speed copyright(c) 2007 kuwata-lab.com all rights reserved.
  9. 9. Overview‣ :‣ : C Ruby‣ : eRuby‣ : C ≦ Ruby ≪≪≪≪≪ C copyright(c) 2007 kuwata-lab.com all rights reserved.
  10. 10. Conclusion‣ •‣ • copyright(c) 2007 kuwata-lab.com all rights reserved.
  11. 11. eRuby Introduction to eRuby copyright(c) 2007 kuwata-lab.com all rights reserved.
  12. 12. eRuby What is eRuby?‣ eRuby (Embedded Ruby) … Ruby ( )‣ eRuby … Ruby Ruby • eruby … C • ERB … pure Ruby Ruby1.8 copyright(c) 2007 kuwata-lab.com all rights reserved.
  13. 13. Example<table> <tbody><% for item in list %> <tr> <td><%= item %></td> </tr><% end %> </tbody> <% ... ... %><table> <%= ... ... %> copyright(c) 2007 kuwata-lab.com all rights reserved.
  14. 14. C eruby Ruby Script Translated by erubyprint "<table>n"print " <tbody>n" for item in list ; print "n"print " <tr>n"print " <td>"; print(( item )); print "</td>n"print " </tr>n" end ; print "n"print " </tbody>n" printprint "<table>n" 1 copyright(c) 2007 kuwata-lab.com all rights reserved.
  15. 15. ERB Ruby Script Translated by ERB_erbout = ""; _erbout.concat "<table>n"_erbout.concat " <tbody>n" for item in list ; _erbout.concat "n"_erbout.concat " <tr>n"_erbout.concat " <td>"; _erbout.concat(( item ).to_s);_erbout.concat "</td>n"_erbout.concat " </tr>n" end ; _erbout.concat "n"_erbout.concat " </tbody>n"_erbout.concat "<table>n"_erbout 1 copyright(c) 2007 kuwata-lab.com all rights reserved.
  16. 16. Output Example<table> <tbody> <tr> <td>AAA</td> </tr> <tr> <td>BBB</td> </tr> <tr> <td>CCC</td> </tr> </tbody> :<table> copyright(c) 2007 kuwata-lab.com all rights reserved.
  17. 17. Benchmark Environment 1: <html>‣ 400 ..... : 53 10,000 53: <table> 54: <% for item in list %> 55: <tr>‣ 10 56: <td><%= item[symbol] %></td> 57: <td><%= item[name] %></td>‣ MacOS X 10.4 Tiger ..... 79: </tr> : 17 (CoreDuo 1.83GHz) x20 80: <% end %>‣ Ruby 1.8.6, eruby 1.0.5 81: </table> ..... :5 85: </html> copyright(c) 2007 kuwata-lab.com all rights reserved.
  18. 18. Benchmark Result50.037.525.012.5 0 C eruby ERB copyright(c) 2007 kuwata-lab.com all rights reserved.
  19. 19. #1:MyEruby#1: First Implementation copyright(c) 2007 kuwata-lab.com all rights reserved.
  20. 20. #1: #1: First Implementation ### Program input.each_line do |line| line.scan(/(.*?)(<%=?|%>)/) do case $2 when <% ; ... 1 when <%= ; ... when %> ; ... ... end end ...http://www.kuwata-lab.com/presen/erubybench-1.1.zip copyright(c) 2007 kuwata-lab.com all rights reserved.
  21. 21. #1: (2) #1: First Implementation (2) Ruby### Translated_buf = ""; _buf << "<html>n";_buf << "<body>n" 1_buf << " <table>n" for item in list ; _buf << "n";_buf << " <tr>n"_buf << " <td>"; _buf << (item[name]).to_s;_buf << "</td>n";_buf << " </tr>n"; end ; _buf << "n";... copyright(c) 2007 kuwata-lab.com all rights reserved.
  22. 22. #1: #1: Benchmark Result100 75 50 25 0 C eruby ERB MyEruby1 copyright(c) 2007 kuwata-lab.com all rights reserved.
  23. 23. #2: #2: Never Split Into Lines copyright(c) 2007 kuwata-lab.com all rights reserved.
  24. 24. #2: #2: Never Split into Lines## Before input.each_line do |line| line.scan(/(.*?)(<%=?|%>)/).each do ... end end## After input.scan(/(.*?)(<%=?|%>)/m).each do ... end copyright(c) 2007 kuwata-lab.com all rights reserved.
  25. 25. #2: (2) #2: Never Split into Lines (2) Ruby## Before_buf << "<html>n"_buf << " <body>n"_buf << " <h1>title</h1>n"## After_buf << <html> <body> <h1>title</h1>; copyright(c) 2007 kuwata-lab.com all rights reserved.
  26. 26. #2: #2: Benchmark Result100 75 50 25 0 C eruby ERB MyEruby1 MyEruby2 copyright(c) 2007 kuwata-lab.com all rights reserved.
  27. 27. #2: #2: Benchmark Result201510 5 0 C eruby MyEruby2 copyright(c) 2007 kuwata-lab.com all rights reserved.
  28. 28. #3: #3: Replace Parsing with Pattern Matching copyright(c) 2007 kuwata-lab.com all rights reserved.
  29. 29. #3: #3: Replace Parsing with Pattern Matching## Beforeinput.scan(/(.*?)(<%=?|%>)/m) do case $2 when <%= ; kind = :expr when <% ; kind = :stmt when %> ; kind = :text ...## Afterinput.scan(/(.*?)<%(=?)(.*?)%>/m) do text, equal, code = $1, $2, $3 s << _convert_str(text, :text) s << _convert_str(code, equal=== ? :expr : :stmt) ... copyright(c) 2007 kuwata-lab.com all rights reserved.
  30. 30. #3: #3: Benchmark Result201510 5 0 C eruby MyEruby2 MyEruby3 copyright(c) 2007 kuwata-lab.com all rights reserved.
  31. 31. #4: #4: Tune Up Regular Expressions copyright(c) 2007 kuwata-lab.com all rights reserved.
  32. 32. #4: #4: Tune Up Regular Expressions## Beforeinput.scan(/(.*?)<%(=)?(.*?)%>/m) do text, equal, code = $1, $2, $3 ...## Afterpos = 0input.scan(/<%(=)?(.*?)%>/m) do equal, code = $1, $2 match = Regexp.last_match len = match.begin(0) - pos text = eruby_str[pos, len] pos = match.end(0) ... copyright(c) 2007 kuwata-lab.com all rights reserved.
  33. 33. #4: #4: Benchmark Result201510 5 0 C eruby MyEruby2 MyEruby3 MyEruby4 copyright(c) 2007 kuwata-lab.com all rights reserved.
  34. 34. #5: #5: Inline Expansion of Method Call copyright(c) 2007 kuwata-lab.com all rights reserved.
  35. 35. #5: #5: Inline Expansion of Method Call### Before ... s << _convert_str(code, :expr) s << _convert_str(code, :stmt) s << _convert_str(text, :text) ...### After ... s << "_buf << (#{code}).to_s; " s << "#{code}; " s << "_buf << #{text.gsub(/[]/, &)}; " ... copyright(c) 2007 kuwata-lab.com all rights reserved.
  36. 36. #5: #5: Benchmark Result201510 5 0 C eruby MyEruby2 MyEruby3 MyEruby4 MyEruby5 copyright(c) 2007 kuwata-lab.com all rights reserved.
  37. 37. #6: #6: Array Buffer copyright(c) 2007 kuwata-lab.com all rights reserved.
  38. 38. #6: #6: Array Buffer Ruby### Before_buf = ;_buf << <td>; _buf << (n).to_s; _buf << </td>;_buf### After String#<<_buf = []; 1 Array#push()_buf.push(<td>, n, </td>);_buf.join copyright(c) 2007 kuwata-lab.com all rights reserved.
  39. 39. #6: #6: Benchmark Result201510 5 0 by 2 3 4 5 6 by by by by by u er ru ru ru ru ru yE yE yE yE yE M M M M copyright(c) 2007 kuwata-lab.com all rights reserved. M C
  40. 40. #7: #7: Interpolationcopyright(c) 2007 kuwata-lab.com all rights reserved.
  41. 41. #7: #7: Interpolation Ruby### Before_buf << <tr><td>; _buf << (n).to_s; _buf << </td></tr>; String#<<### After_buf << %Q`<tr><td>#{n}</td> "...str..."</tr>` %Q`...str...` copyright(c) 2007 kuwata-lab.com all rights reserved.
  42. 42. #7: #7: Benchmark Result201510 5 0 y 2 3 4 5 6 7 ub by by by by by by er ru ru ru ru ru ru yE yE yE yE yE yE M M M M M copyright(c) 2007 kuwata-lab.com all rights reserved. M C
  43. 43. #8: #7: File Caching copyright(c) 2007 kuwata-lab.com all rights reserved.
  44. 44. #8: #8: File Caching## Afterdef convert_file(filename, cachename=nil) cachename ||= filename + .cache if or prog = convert(File.read(filename)) (prog) else prog = end return prog Rubyend copyright(c) 2007 kuwata-lab.com all rights reserved.
  45. 45. #8: #8: Benchmark Result201510 5 0 y 2 3 4 5 6 7 8 ub by by by by by by by er ru ru ru ru ru ru ru yE yE yE yE yE yE yE M M M M M M M copyright(c) 2007 kuwata-lab.com all rights reserved. C
  46. 46. #9: #7: Make Methods copyright(c) 2007 kuwata-lab.com all rights reserved.
  47. 47. #9: #9: Make Methods### Before prog = myeruby.convert_file(filename) eval prog### After Ruby def define_method(body, args=[]) eval "def self.evaluate(#{args.join(,)}); #{body}; end" end prog = myeruby.convert_file(filename) args = [list] myeruby.define_method(prog, args) myeruby.evaluate() copyright(c) 2007 kuwata-lab.com all rights reserved.
  48. 48. #9: #9: Benchmark Result201510 5 0 y 2 3 4 5 6 7 8 9 ub by by by by by by by by er ru ru ru ru ru ru ru ru yE yE yE yE yE yE yE yE M M M M M M M M copyright(c) 2007 kuwata-lab.com all rights reserved. C
  49. 49. Principles of Tuning :##2 1 69.17#8 2.44#9 2.26#2 1 4.91#5 2.11#6 Array#push() 0.94#7 0.94#3 4.20#4 2.12 copyright(c) 2007 kuwata-lab.com all rights reserved.
  50. 50. Comparison with Competitors Lang Solution Time(sec)Ruby eruby 15.32 ERB 42.67 MyEruby7 (interporation) 10.42 MyEruby8 (cached) 8.02 MyEruby9 (method) 5.11 Perl Template-Toolkit 26.40Python Django 50.55 Kid (TurboGears) 344.16 Java Jakarta Velocity 13.24 copyright(c) 2007 kuwata-lab.com all rights reserved.
  51. 51. Summarycopyright(c) 2007 kuwata-lab.com all rights reserved.
  52. 52. Result‣C 1.5 Ruby • 2 3 •‣ • Java Ruby copyright(c) 2007 kuwata-lab.com all rights reserved.
  53. 53. Conclusion‣ •‣ • copyright(c) 2007 kuwata-lab.com all rights reserved.
  54. 54. one more minute... copyright(c) 2007 kuwata-lab.com all rights reserved.
  55. 55. Erubis • ERB 3 eruby 10% •eRuby • <% %> • <%= %> HTML escape • YAML • Ruby • (C/Java/PHP/Perl/Scheme/JavaSript) • RoR 30% ( ) copyright(c) 2007 kuwata-lab.com all rights reserved.
  56. 56. thank youcopyright(c) 2007 kuwata-lab.com all rights reserved.

×