matplotlibを使って計算時間をプロットしてみた

matplotlibを使って、JavaのHashSetとArrayListのcontainsの実行速度を測った結果を図にしました。loglogグラフなので、ArrayListはO(N)でHashSetはO(1)でしょうか。測定方法とテストの詳細は「実装パターン」の付録Aに載っています。

使ったデータ

N 1 10 100 1000 10000 100000
setMembership 7.26 9.97 10.49 10.94 11.52 11.21
arrayListMembership 6.65 29.72 228.36 2585.26 35996.90 1257285.81

matplotlibのコード

#!/usr/bin/env python2.7
#encoding:utf-8
from matplotlib import pyplot
with open("set_arraylist.data") as f:
  x = [int(d) for d in f.readline().rstrip().split(" ")[1:]]
  ys = []
  legends = []
  for line in f:
    data = line.rstrip().split(" ")
    legends.append(data[0])
    ys.append([float(fl) for fl in data[1:]])
  size = len(x)
  for y in ys:
    if len(y) == size:
     pyplot.plot(x, y)
  pyplot.legend(legends)
  pyplot.xlabel("#elements")
  pyplot.ylabel("processing time(ns)")
  pyplot.xscale("log")
  pyplot.yscale("log")
  pyplot.show()

matplotlibのインストール

環境はUbuntu 12.04です。pipでも入りましたが、showでGUIが立ち上がりませんでした。色々試しましたが結局apt-getで一発で入りました。公式サイトに書いてあるのと同じ方法です。

$ apt-get install python-matplotlib