Üniversitede Bioinformatics dersini alırken oldukça eğlenceli oyunlar/problemler gördük. Bunlardan birisi de Towers of Hanoi – Hanoi Kuleleri idi. Dönemin başında bol vakit olduğu için de oturup bu problemi çözen C, Python ve Java kodu yazmıştım. Ardından da bu dillerde yazılan eş-işlevli programların performansını değerlendirmiştim. Kodları ve sonuçları sizinle paylaşmak istedim. HANOITOWERS fonksiyonlarına gönderilen parametreler ise (öğe sayısı, hangi çubuktan, hangi çubuğa).
C Kodu
#include int HANOITOWERS(int n, int fromPeg, int toPeg); int main() { printf("Hello world!\n"); HANOITOWERS(20, 1, 3); return 0; } int HANOITOWERS(int n, int fromPeg, int toPeg){ int unusedPeg; if (n == 1){ printf("Move disk from peg %d to peg %d\n", fromPeg, toPeg); return 0; } unusedPeg = 6 - fromPeg - toPeg; HANOITOWERS(n-1, fromPeg, unusedPeg); printf("Move disk from peg %d to peg %d\n", fromPeg, toPeg); HANOITOWERS(n-1, unusedPeg, toPeg); return 0; }
Python Kodu
def HANOITOWERS(n, fromPeg, toPeg): if n==1: print "Move disk from peg %d to peg %d" % (fromPeg, toPeg) return unusedPeg = 6 - fromPeg - toPeg HANOITOWERS(n-1, fromPeg, unusedPeg) print "Move disk from peg %d to peg %d" % (fromPeg, toPeg) HANOITOWERS(n-1, unusedPeg, toPeg) return HANOITOWERS(20, 1, 3)
Java Kodu
public class Hanoi { public static void main(String args[]){ Hanoi y = new Hanoi(); y.HANOITOWERS(20, 1, 3); } public Hanoi(){ } public int HANOITOWERS(int n, int fromPeg, int toPeg){ int unusedPeg; if (n == 1){ System.out.printf("Move disk from peg %d to peg %d\n", fromPeg, toPeg); return 0; } unusedPeg = 6 - fromPeg - toPeg; HANOITOWERS(n-1, fromPeg, unusedPeg); System.out.printf("Move disk from peg %d to peg %d\n", fromPeg, toPeg); HANOITOWERS(n-1, unusedPeg, toPeg); return 0; } }
|
|||||
Test Sonuçları:
| Dil | n=3 | n=10 | n=20 |
|---|---|---|---|
| Python | 0.016 | 0.020 | 8.027 |
| C | 0.001 | 0.007 | 7.636 |
| Java | 0.105 | 0.308 | 18.945 |
Görüldüğü üzere Python ve C her halükarda Java’dan daha hızlı. Python’un yorumlayıcı başlatma süresi küçük problemlerde C’den çok yavaş kalmasına sebep olurken problemler büyüdükçe C ve Python arasındaki fark azalmakta. Bilimsel hesaplamalarda Python’un kullanılmasına şaşmamak gerek.
As it can be seen, Python and C is faster than Java in all the problem size cases. Though the initialization time of Python Interpreter slows Python benchmark compared to C, as the problem gets bigger, the difference between Python and C becomes much more smaller. It’s not surprising that Python is being used in scientific calculations.

13.10 on Temmuz 31st, 2009
Tamam havanı atmışsın anladık. Gel birde benim problemleri çöz Emre
14.42 on Temmuz 31st, 2009
=) Çözeriz abi ayıp ettin
14.47 on Ağustos 9th, 2009
Merhabalar Emre senden bu tarz yazilarda bekliyoruz. Yeteneklerin gizli kalmasin
Sevgilerle
12.07 on Eylül 29th, 2009
Merhaba Emre,
bu testi paylaştığın için teşekkür ederim.
Ancak sormak istediğim yazdıklarından hangisi geçerli. bilimsel hesaplamalarda python kullanılmaktamı kullanılmamaktamı
“Bilimsel hesaplamalarda Python’un kullanılmamasına şaşmamak gerek.”
“It’s not surprising that Python is being used in scientific calculations.”
12.11 on Eylül 29th, 2009
Merhaba,
Kullanılmakta. Fazladan bir “ma” koymuşum, düzelttim şimdi. =) Teşekkürler.
0.24 on Mart 6th, 2010
[...] [...]