前回の記事で、IPアドレスのソートについて書きました。

今回はその続きで、takiuchiさんに教えてもらったものや、自分で書いたものの性能比較をしようと思います。

まず、前回の記事&コメントでどのようなソート方法があったかというと、

①自分で書いたソート(以下, my_sort_1)

   1  ip_addrs.sort_by{|a| a.split(".").map{|i| "%3d" % i.to_i}}

②自分で書いたソート・改(以下, my_sort_2)

   1  ip_addrs.sort_by{|a| a.split(".").map{|i| i.to_i}}

③takiuchiさんに教えていただいたソート(以下, takiuchi_sort)

   1  ip_addrs.sort_by{|i| Socket.sockaddr_in nil, i}

の3つです。 これらについて、ベンチマークをとってみます。

ソートする対象としては、世界の国別 IPv4 アドレス割り当てリストを使いました。

まず、下準備として、上記リストには、サブネットマスク版とCIDR表記版があるので、ここからIPアドレスのみを抽出し、Array型のオブジェクトに格納ておきます(このオブジェクトをip_addrsとする)。

全部で、51814のIPアドレスがあるらしいです。

そして、ベンチマークをとってみます。 -コード

   1  require 'benchmark'
   2  Benchmark.bm do |x|
   3    x.report("my_sort_1    :"){ ip_addrs.sort_by{|a| a.split(".").map{|i| "%3d" % i.to_i}}}
   4    x.report("my_sort_2    :"){ ip_addrs.sort_by{|a| a.split(".").map{|i| i.to_i}}}
   5    x.report("takiuchi_sort:"){ ip_addrs.sort_by{|i| Socket.sockaddr_in nil, i}}
   6  end

-結果

   1        user     system      total        real
   2  my_sort_1    :  3.860000   1.160000   5.020000 (  5.017623)
   3  my_sort_2    :  2.170000   0.650000   2.820000 (  2.819594)
   4  takiuchi_sort:  0.690000   0.180000   0.870000 (  0.864372)

takiuchiさんに教えていただいたソートが圧倒的に速いですね!! ここまで差がでるとは、正直思っていなかった。

自分で書いたmy_sort_1とmy_sort2でもかなりの差がみられました。

まぁ、my_sort_1のほうは無駄が多いですしね・・・。

こんな感じで、結論としては、 takiuchiさんの、

   1  ip_addrs.sort_by{|i| Socket.sockaddr_in nil, i}
がベンチマーク的に優秀で、コードの見た目もシンプルでかなり良さげです!!

他に「こんな方法のソートがあるよ!」というのがあれば、教えていただけると嬉しいです!

posted by Png y_tsuda on Wed 24 Sep 2008 at 17:01

Recently, the Formula had been slowing down.

The Formula is the service which provides a way to make images of mathematical expressions for embedding it into your blogs, BBSs and so on.

There was the problem in the manner of ranking the referred formulae. So I solved the issue by choosing simpler algorithm.

Now it became faster. Please try it again if you've given up once before for the slowness of the service.

Thank you for patience. I will keep appreciating any kind of feedbacks. Feel free to contact us.

posted by Png takiuchi on Wed 24 Sep 2008 at 03:48

config/environment.rbの中で、config.gemによってversion指定したgemを、 rake gems:unpackしようとすると、以下のようなエラーが出るようです。

   1  % rake gems:unpack GEM=genki-pagination_scope 
   2  (in /Users/takiuchi/project/formula)
   3  ERROR:  While executing gem ... (ArgumentError)
   4      Illformed requirement ["\">= 0.0.4\""]

RailsのLightHouseでも報告されていました。

rake gems:unpack version handling broken

ということで、この問題を回避するために、上記Ticketに添付されていた Patchを参考に、以下のようなMonkeyPatchを作ってみました。

config/initializers/fix_gem_unpack_for_2_1_1.rb

   1  module Rails
   2    class GemDependency
   3      def unpack_to(directory)
   4        FileUtils.mkdir_p directory
   5        Dir.chdir directory do
   6          Gem::GemRunner.new.run(
   7            unpack_command.map {|i| i.gsub(/\A["']|["']\z/, '')})
   8        end
   9  
  10        spec_filename = File.join(gem_dir(directory), '.specification')
  11        File.open(spec_filename, 'w') do |file|
  12          file.puts specification.to_yaml
  13        end
  14      end
  15    end
  16  end

これをconfig/initializers/の下に置いておけば、とりあえず正常にunpackする事ができます。

posted by Png genki on Wed 24 Sep 2008 at 02:10
Contents
IPアドレスをソートする (性能比較編)
Sped up the Formula
Rails-2.1でversion指定したGemをunpackできない問題
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ