就讓 Raspberry Pi 來計算 Pi 吧

benchmark 在各式各樣的電子產品上面一直是很重要的項目,因為處理器有成千上萬種,光是常常聽到的廠牌就有 Intel/AMD/Arm/Cyris/Mips/VIA,這之間或許因為架構不同,沒辦法在同一個基礎上面比較(通常也沒有人會這樣比),但是光是同一個廠牌就可能有上千種型號,比如 Intel 同一時期的 CPU 就有分成 i3/i5/i7....,而一個 i3 又有 Westmere/Sandy Bridge/Ivy Bridge,再細分 Ivy Bridge 又有 3240T/3240/3120M ,這麼多種處理器之間要怎麼知道你所購買的是不是符合需求呢?Sandy Bridge 2130 比較快還是 Ivy Bridge 3240比較快呢?這一切都要靠測速程式來幫大家判斷。
同樣的 Raspberry Pi 也有不同的版本,我手中的版本是 Model B Revision 2.0 (512MB RAM) ,系統預設:CPU速度700MHz;記憶體分割gpu_mem=64 : 64M GPU, 192M/448M ARM split。
既然 少年 Raspberry Pi 在電影裡面很會背 Pi,那我們就叫他算一下 Pi 吧,接下來我們要寫一個計算 Pi的程式,請先打開 console 輸入指令
nano pi

再把這一些程式碼貼上
"""
Python3 program to Calculate 100 decimal places of PI using Archimedes
method and decimal numbers

See: http://www.craig-wood.com/nick/articles/nick/articles/pi-archimedes/

By Nick Craig-Wood <nick@craig-wood.com>

We inscribe a square in a circle of unit radius initially and
calculate half the circumference of the square to approximate pi.

We then divide each segment of the polygon in two and find the new
length of the polygon using the recurrence relation

new_edge_length = sqrt(2 - 2 * sqrt(1 - (edge_length ** 2) / 4))

It is simpler to calculate the square of the polygon_edge length, and
square root it at the end

new_edge_length_squared = 2 - 2 * sqrt(1 - edge_length_squared / 4))
"""
import datetime
from decimal import Decimal, getcontext

def pi_archimedes(n):
"""
Calculate n iterations of Archimedes PI recurrence relation
"""
polygon_edge_length_squared = Decimal(2)
polygon_sides = 2
for i in range(n):
polygon_edge_length_squared = 2 - 2 * (1 - polygon_edge_length_squared / 4).sqrt()
polygon_sides *= 2
return polygon_sides * polygon_edge_length_squared.sqrt()
def pi_archimedes(n):
"""
Calculate n iterations of Archimedes PI recurrence relation
"""
polygon_edge_length_squared = Decimal(2)
polygon_sides = 2
for i in range(n):
polygon_edge_length_squared = 2 - 2 * (1 - polygon_edge_length_squared / 4).sqrt()
polygon_sides *= 2
return polygon_sides * polygon_edge_length_squared.sqrt()

def main():
"""
Try the series
"""
places = 100
old_result = None
for n in range(10*places):
# Do calculations with double precision
getcontext().prec = 2*places
result = pi_archimedes(n)
# Print the result with single precision
getcontext().prec = places
result = +result # do the rounding on result
print("%3d: %s" % (n, result))
if result == old_result:
break
old_result = result
if __name__ == "__main__":
startTime = datetime.datetime.now()
main()
elapsed=datetime.datetime.now()-startTime
print " Total Time: %s" %(elapsed)

按下 Ctrl + O 儲存,Ctrl + X 離開,接下來就可以執行了
如果上面程式碼複製的有問題,那就直接另存新檔:pi_archimedes_decimal.py
python pi

這個程式會計算到小數點以下100位數
700 Mhz
Total TIme: 0:01:16.631661

順便來玩一下超頻好了
800 Mhz
Total TIme: 0:01:10.652900


900 Mhz
Total TIme: 0:01:05.422762


950 Mhz
Total TIme: 0:01:04.727769

真是奇怪,950Mhz 和 1000Mhz 怎麼就差這麼多啊
1000 Mhz
Total TIme: 0:00:48.695088

如果有興趣的人可以貼上你的測試結果和我對照一下

BLOG
2013-01-22 23:16 發佈
文章關鍵字 raspberry pi
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?