2013年12月19日 星期四

更動dict內容的效能比較 ( Compare performance of dict updating )

字典(dict)是Python內建的資料型態物件(Built-in Types)
關於dict的宣告、使用方式等,有興趣的讀者請自行參考Python官方文件
網路上亦有許多資料,此不贅述

本文試圖比較兩種更動dict的方法的效能

作法1. 使用dict內建的update method
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx.update({'a':5})
作法2. 使用指定(assign)值的方式
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx['a'] = 5
寫點小程式比較兩者的效能:
from __future__ import print_function
def dict_update():
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx.update({'a':5})
def dict_assign():
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx['a'] = 5
if __name__ == '__main__':
    import timeit
    tUpdate = timeit.timeit("dict_update()", setup="from __main__ import dict_update")
    tAssign = timeit.timeit("dict_assign()", setup="from __main__ import dict_assign")
    print ("Time of dict_update(): ", tUpdate)
    print ("Time of dict_assign(): ", tAssign)
註解:
1. 匯入"from __future__ import print_function"令Python2.7得以使用Python3的print method
2. timeit是Python裡用來計算執行時間的模塊(module),可用來計算function的執行時間,也可用來計算一小段代碼的執行時間,有興趣的讀者可自行參考Python官方文件,裡面可以找到timeit.timeit的用法

環境:
作業系統:Windows 7
Python編譯器版本:2.7.5

結果:
作法1. dict內建的update方法:0.51995......sec
作法2. 使用指定值的方式:0.26610......sec

雖然使用指定值的方式效能會比dict.update()方式快,但dict.update()可以用一行程式碼更動dict內多個key的值,特別是在dict內的key很多,一次需要更動大量的值時,dict.update()具有編寫程式的方便性的效益就顯現出來。孰優孰劣?請各位看倌自行判斷。

English Version:


"dict" is Built-in Types of Python.
Please refer to Python Documentation for dict usage
Here practice 2 approaches to update the value of dict, and compare the performance between them.
1. Use dict built-in method, "update":
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx.update({'a':5})
2. Assign the value in the dict:
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx['a'] = 5
Let's compare the performance between them via the code of the following:
from __future__ import print_function
def dict_update():
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx.update({'a':5})
def dict_assign():
    dictEx = {'a':1, 'b':2, 'c':3}
    dictEx['a'] = 5
if __name__ == '__main__':
    import timeit
    tUpdate = timeit.timeit("dict_update()", setup="from __main__ import dict_update")
    tAssign = timeit.timeit("dict_assign()", setup="from __main__ import dict_assign")
    print ("Time of dict_update(): ", tUpdate)
    print ("Time of dict_assign(): ", tAssign)
Remark:
1. Invoke "from __future__ import print_function" on the Python2.7 can let programmer use print function as Python3
2. "timeit" is a Python module which can measure execution time of the code snippets. Please refer to Python Documentation for timeit usage. There is timeit.timeit on the inside.

Execute environment:
OS: Windows 7
Python version: 2.7.5

List the execution time with 1 million repetitions:
1. Use dict built-in method, "update": 0.51995......sec
2. Assign the value in the dict: 0.26610......sec

Execution time of approach 2 (Assign the value in the dict) is faster than approach 1 (Use dict built-in method, "update"). However, dict.update() could update several values in one line code. It is convenient for programmer while they need to update a lot of values on a dict.

沒有留言:

張貼留言