SlideShare una empresa de Scribd logo
1 de 56
RubyKaigi 2007 Session



C                      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.
…



copyright(c) 2007 kuwata-lab.com all rights reserved.
2007
         Script Languages in 2007




       copyright(c) 2007 kuwata-lab.com all rights reserved.
PS3
         Break Away from PS3 Syndrome




‣ PS3/PSP …
  •

‣ Wii/DS …
  •


           copyright(c) 2007 kuwata-lab.com all rights reserved.
Script Languages can't be Center Player


‣                                                                 Java
    • Java
    •                                                          ≠


‣
    •
    •
              copyright(c) 2007 kuwata-lab.com all rights reserved.
Problems and Truth about Script Languages




✓

✓

✓



           copyright(c) 2007 kuwata-lab.com all rights reserved.
About This Presentation




copyright(c) 2007 kuwata-lab.com all rights reserved.
Point




                      ≠
Language Speed ≠ Application Speed




      copyright(c) 2007 kuwata-lab.com all rights reserved.
Overview



‣   :

‣   : C                       Ruby

‣   : eRuby

‣   :                    C
        ≦                              Ruby
        ≪≪≪≪≪                                                     C


          copyright(c) 2007 kuwata-lab.com all rights reserved.
Conclusion



‣

    •

‣
    •


        copyright(c) 2007 kuwata-lab.com all rights reserved.
eRuby
     Introduction to eRuby




 copyright(c) 2007 kuwata-lab.com all rights reserved.
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.
Example


<table>
 <tbody>
<% for item in list %>
  <tr>
    <td><%= item %></td>
  </tr>
<% end %>
 </tbody>            <% ... ... %>
<table>              <%= ... ... %>



      copyright(c) 2007 kuwata-lab.com all rights reserved.
C         eruby
        Ruby Script Translated by eruby


print "<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"                print
print "<table>n"                  1


          copyright(c) 2007 kuwata-lab.com all rights reserved.
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.
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.
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.
Benchmark Result



50.0


37.5


25.0


12.5


  0
       C     eruby                                         ERB

           copyright(c) 2007 kuwata-lab.com all rights reserved.
#1:
MyEruby#1: First Implementation




  copyright(c) 2007 kuwata-lab.com all rights reserved.
#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.
#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.
#1:
                    #1: Benchmark Result



100


 75


 50


 25


  0
      C     eruby                      ERB                          MyEruby1

                copyright(c) 2007 kuwata-lab.com all rights reserved.
#2:
       #2: Never Split Into Lines




      copyright(c) 2007 kuwata-lab.com all rights reserved.
#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.
#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.
#2:
                          #2: Benchmark Result




100


 75


 50


 25


  0
      C    eruby             ERB                  MyEruby1                 MyEruby2

                   copyright(c) 2007 kuwata-lab.com all rights reserved.
#2:
                  #2: Benchmark Result




20


15


10


 5


 0
       C      eruby                                    MyEruby2

           copyright(c) 2007 kuwata-lab.com all rights reserved.
#3:
  #3: Replace Parsing with Pattern Matching




         copyright(c) 2007 kuwata-lab.com all rights reserved.
#3:
       #3: Replace Parsing with Pattern Matching

## Before
input.scan(/(.*?)(<%=?|%>)/m) do
  case $2
  when '<%=' ; kind = :expr
  when '<%' ; kind = :stmt
  when '%>' ; kind = :text
  ...

## After
input.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.
#3:
                   #3: Benchmark Result



20


15


10


 5


 0
      C    eruby                 MyEruby2                            MyEruby3

             copyright(c) 2007 kuwata-lab.com all rights reserved.
#4:
      #4: Tune Up Regular Expressions




        copyright(c) 2007 kuwata-lab.com all rights reserved.
#4:
         #4: Tune Up Regular Expressions

## Before
input.scan(/(.*?)<%(=)?(.*?)%>/m) do
  text, equal, code = $1, $2, $3
  ...

## After
pos = 0
input.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.
#4:
                      #4: Benchmark Result




20


15


10


 5


 0
     C    eruby       MyEruby2                 MyEruby3                MyEruby4

               copyright(c) 2007 kuwata-lab.com all rights reserved.
#5:
  #5: Inline Expansion of Method Call




      copyright(c) 2007 kuwata-lab.com all rights reserved.
#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.
#5:
                      #5: Benchmark Result




20


15


10


 5


 0
     C    eruby MyEruby2           MyEruby3            MyEruby4        MyEruby5

               copyright(c) 2007 kuwata-lab.com all rights reserved.
#6:
                 #6: Array Buffer




      copyright(c) 2007 kuwata-lab.com all rights reserved.
#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.
#6:
                      #6: Benchmark Result



20


15


10


 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
#7:
          #7: Interpolation




copyright(c) 2007 kuwata-lab.com all rights reserved.
#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.
#7:
                        #7: Benchmark Result




20


15


10


 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
#8:
                  #7: File Caching




      copyright(c) 2007 kuwata-lab.com all rights reserved.
#8:
                      #8: File Caching


## After
def convert_file(filename, cachename=nil)
 cachename ||= filename + '.cache'
 if                               or
    prog = convert(File.read(filename))
                                     (prog)
 else
    prog =
 end
 return prog                    Ruby
end

            copyright(c) 2007 kuwata-lab.com all rights reserved.
#8:
               #8: Benchmark Result



20


15


10


 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
#9:
          #7: Make Methods




  copyright(c) 2007 kuwata-lab.com all rights reserved.
#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.
#9:
               #9: Benchmark Result



20


15


10


 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
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.
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.40

Python    Django                                                     50.55
          Kid (TurboGears)                                          344.16
 Java     Jakarta Velocity                                           13.24
           copyright(c) 2007 kuwata-lab.com all rights reserved.
Summary




copyright(c) 2007 kuwata-lab.com all rights reserved.
Result



‣C      1.5                    Ruby
    •                        2                                    3

    •

‣
    •    Java             Ruby


          copyright(c) 2007 kuwata-lab.com all rights reserved.
Conclusion



‣

    •

‣
    •


        copyright(c) 2007 kuwata-lab.com all rights reserved.
one more minute...



   copyright(c) 2007 kuwata-lab.com all rights reserved.
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.
thank you



copyright(c) 2007 kuwata-lab.com all rights reserved.

Más contenido relacionado

La actualidad más candente

Advanced python
Advanced pythonAdvanced python
Advanced python
EU Edge
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
julien pauli
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 

La actualidad más candente (20)

Working with databases in Perl
Working with databases in PerlWorking with databases in Perl
Working with databases in Perl
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
 
How to Begin Developing Ruby Core
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby Core
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
 
Advanced python
Advanced pythonAdvanced python
Advanced python
 
Diving into HHVM Extensions (php[tek] 2016)
Diving into HHVM Extensions (php[tek] 2016)Diving into HHVM Extensions (php[tek] 2016)
Diving into HHVM Extensions (php[tek] 2016)
 
What you need to remember when you upload to CPAN
What you need to remember when you upload to CPANWhat you need to remember when you upload to CPAN
What you need to remember when you upload to CPAN
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
typemap in Perl/XS
typemap in Perl/XS  typemap in Perl/XS
typemap in Perl/XS
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
閒聊Python應用在game server的開發
閒聊Python應用在game server的開發閒聊Python應用在game server的開發
閒聊Python應用在game server的開發
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2
 

Similar a Cより速いRubyプログラム

Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
tomaspavelka
 

Similar a Cより速いRubyプログラム (20)

The details of CI/CD environment for Ruby
The details of CI/CD environment for RubyThe details of CI/CD environment for Ruby
The details of CI/CD environment for Ruby
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
 
Quick Intro To JRuby
Quick Intro To JRubyQuick Intro To JRuby
Quick Intro To JRuby
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
Ruby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programingRuby on Rails 3.1: Let's bring the fun back into web programing
Ruby on Rails 3.1: Let's bring the fun back into web programing
 
Ruby 1.9
Ruby 1.9Ruby 1.9
Ruby 1.9
 
New features in Ruby 2.5
New features in Ruby 2.5New features in Ruby 2.5
New features in Ruby 2.5
 
MySQLドライバの改良と軽量O/Rマッパーの紹介
MySQLドライバの改良と軽量O/Rマッパーの紹介MySQLドライバの改良と軽量O/Rマッパーの紹介
MySQLドライバの改良と軽量O/Rマッパーの紹介
 
Sinatra
SinatraSinatra
Sinatra
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo Web
 
Headless Js Testing
Headless Js TestingHeadless Js Testing
Headless Js Testing
 
How to test code with mruby
How to test code with mrubyHow to test code with mruby
How to test code with mruby
 
Whatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the processWhatever it takes - Fixing SQLIA and XSS in the process
Whatever it takes - Fixing SQLIA and XSS in the process
 
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxRubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
 
Static Code Analysis PHP[tek] 2023
Static Code Analysis PHP[tek] 2023Static Code Analysis PHP[tek] 2023
Static Code Analysis PHP[tek] 2023
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby Core
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret Sauce
 
Debugging TV Frame 0x13
Debugging TV Frame 0x13Debugging TV Frame 0x13
Debugging TV Frame 0x13
 

Más de kwatch

文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた
kwatch
 
I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"
kwatch
 
How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -
How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -
How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -
kwatch
 

Más de kwatch (20)

How to make the fastest Router in Python
How to make the fastest Router in PythonHow to make the fastest Router in Python
How to make the fastest Router in Python
 
Migr8.rb チュートリアル
Migr8.rb チュートリアルMigr8.rb チュートリアル
Migr8.rb チュートリアル
 
なんでもID
なんでもIDなんでもID
なんでもID
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
 
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
【SQLインジェクション対策】徳丸先生に怒られない、動的SQLの安全な組み立て方
 
O/Rマッパーによるトラブルを未然に防ぐ
O/Rマッパーによるトラブルを未然に防ぐO/Rマッパーによるトラブルを未然に防ぐ
O/Rマッパーによるトラブルを未然に防ぐ
 
正規表現リテラルは本当に必要なのか?
正規表現リテラルは本当に必要なのか?正規表現リテラルは本当に必要なのか?
正規表現リテラルは本当に必要なのか?
 
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
【公開終了】Python4PHPer - PHPユーザのためのPython入門 (Python2.5)
 
DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!DBスキーマもバージョン管理したい!
DBスキーマもバージョン管理したい!
 
PHPとJavaScriptにおけるオブジェクト指向を比較する
PHPとJavaScriptにおけるオブジェクト指向を比較するPHPとJavaScriptにおけるオブジェクト指向を比較する
PHPとJavaScriptにおけるオブジェクト指向を比較する
 
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か?
 
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
 
PHP5.5新機能「ジェネレータ」初心者入門
PHP5.5新機能「ジェネレータ」初心者入門PHP5.5新機能「ジェネレータ」初心者入門
PHP5.5新機能「ジェネレータ」初心者入門
 
Pretty Good Branch Strategy for Git/Mercurial
Pretty Good Branch Strategy for Git/MercurialPretty Good Branch Strategy for Git/Mercurial
Pretty Good Branch Strategy for Git/Mercurial
 
Oktest - a new style testing library for Python -
Oktest - a new style testing library for Python -Oktest - a new style testing library for Python -
Oktest - a new style testing library for Python -
 
文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた文字列結合のベンチマークをいろんな処理系でやってみた
文字列結合のベンチマークをいろんな処理系でやってみた
 
I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"I have something to say about the buzz word "From Java to Ruby"
I have something to say about the buzz word "From Java to Ruby"
 
Javaより速いLL用テンプレートエンジン
Javaより速いLL用テンプレートエンジンJavaより速いLL用テンプレートエンジン
Javaより速いLL用テンプレートエンジン
 
Underlaying Technology of Modern O/R Mapper
Underlaying Technology of Modern O/R MapperUnderlaying Technology of Modern O/R Mapper
Underlaying Technology of Modern O/R Mapper
 
How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -
How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -
How to Make Ruby CGI Script Faster - CGIを高速化する小手先テクニック -
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Cより速いRubyプログラム

  • 1. RubyKaigi 2007 Session C 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.
  • 3. 2007 Script Languages in 2007 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 4. PS3 Break Away from PS3 Syndrome ‣ PS3/PSP … • ‣ Wii/DS … • copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 5. Script Languages can't be Center Player ‣ Java • Java • ≠ ‣ • • copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 6. Problems and Truth about Script Languages ✓ ✓ ✓ copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 7. About This Presentation copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 8. Point ≠ Language Speed ≠ Application Speed copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 9. Overview ‣ : ‣ : C Ruby ‣ : eRuby ‣ : C ≦ Ruby ≪≪≪≪≪ C copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 10. Conclusion ‣ • ‣ • copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 11. eRuby Introduction to eRuby copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. 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. C eruby Ruby Script Translated by eruby print "<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" print print "<table>n" 1 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. 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. 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. Benchmark Result 50.0 37.5 25.0 12.5 0 C eruby ERB copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 19. #1: MyEruby#1: First Implementation copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. #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. #1: #1: Benchmark Result 100 75 50 25 0 C eruby ERB MyEruby1 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 23. #2: #2: Never Split Into Lines copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. #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. #2: #2: Benchmark Result 100 75 50 25 0 C eruby ERB MyEruby1 MyEruby2 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 27. #2: #2: Benchmark Result 20 15 10 5 0 C eruby MyEruby2 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 28. #3: #3: Replace Parsing with Pattern Matching copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 29. #3: #3: Replace Parsing with Pattern Matching ## Before input.scan(/(.*?)(<%=?|%>)/m) do case $2 when '<%=' ; kind = :expr when '<%' ; kind = :stmt when '%>' ; kind = :text ... ## After input.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. #3: #3: Benchmark Result 20 15 10 5 0 C eruby MyEruby2 MyEruby3 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 31. #4: #4: Tune Up Regular Expressions copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 32. #4: #4: Tune Up Regular Expressions ## Before input.scan(/(.*?)<%(=)?(.*?)%>/m) do text, equal, code = $1, $2, $3 ... ## After pos = 0 input.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. #4: #4: Benchmark Result 20 15 10 5 0 C eruby MyEruby2 MyEruby3 MyEruby4 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 34. #5: #5: Inline Expansion of Method Call copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. #5: #5: Benchmark Result 20 15 10 5 0 C eruby MyEruby2 MyEruby3 MyEruby4 MyEruby5 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 37. #6: #6: Array Buffer copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. #6: #6: Benchmark Result 20 15 10 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. #7: #7: Interpolation copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. #7: #7: Benchmark Result 20 15 10 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. #8: #7: File Caching copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 44. #8: #8: File Caching ## After def convert_file(filename, cachename=nil) cachename ||= filename + '.cache' if or prog = convert(File.read(filename)) (prog) else prog = end return prog Ruby end copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 45. #8: #8: Benchmark Result 20 15 10 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. #9: #7: Make Methods copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. #9: #9: Benchmark Result 20 15 10 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. 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. 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.40 Python Django 50.55 Kid (TurboGears) 344.16 Java Jakarta Velocity 13.24 copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 52. Result ‣C 1.5 Ruby • 2 3 • ‣ • Java Ruby copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 53. Conclusion ‣ • ‣ • copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 54. one more minute... copyright(c) 2007 kuwata-lab.com all rights reserved.
  • 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. thank you copyright(c) 2007 kuwata-lab.com all rights reserved.