[ruby-cvs:66792] naruse:r59614 (trunk): Add optimization for creating zerofill string

naruse at ruby-lang.org naruse at ruby-lang.org
Fri Aug 18 01:34:40 JST 2017


naruse	2017-08-18 01:34:40 +0900 (Fri, 18 Aug 2017)

  New Revision: 59614

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59614

  Log:
    Add optimization for creating zerofill string
    
    ```
    require 'benchmark'
    n = 1 * 1024 * 1024 * 1024
    Benchmark.bmbm do |x|
      x.report("*") { 0.chr * n }
      x.report("ljust") { String.new(capacity: n).ljust(n, "\0") }
    end
    ```
    
    Before
    
    ```% ./ruby test.rb
    Rehearsal -----------------------------------------
    *       0.358396   0.392753   0.751149 (  1.134231)
    ljust   0.203277   0.389223   0.592500 (  0.594816)
    -------------------------------- total: 1.343649sec
    
                user     system      total        real
    *       0.282647   0.304600   0.587247 (  0.589205)
    ljust   0.201834   0.283801   0.485635 (  0.487617)
    ```
    
    After
    
    ```% ./ruby test.rb
    Rehearsal -----------------------------------------
    *       0.000522   0.000021   0.000543 (  0.000534)
    ljust   0.208551   0.321030   0.529581 (  0.542083)
    -------------------------------- total: 0.530124sec
    
                user     system      total        real
    *       0.000069   0.000006   0.000075 (  0.000069)
    ljust   0.206698   0.301032   0.507730 (  0.517674)
    ```

  Modified files:
    trunk/string.c


More information about the ruby-cvs mailing list