Category: Türkçe

Date:

Ü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 ;
}
 
int HANOITOWERS(int n, int fromPeg, int toPeg){
 
    int unusedPeg;
 
    if (n == 1){
        printf("Move disk from peg %d to peg %dn", fromPeg, toPeg);
        return ;
    }
    unusedPeg = 6 - fromPeg - toPeg;
    HANOITOWERS(n-1, fromPeg, unusedPeg);
    printf("Move disk from peg %d to peg %dn", fromPeg, toPeg);
    HANOITOWERS(n-1, unusedPeg, toPeg);
    return ;
 
}

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 %dn", fromPeg, toPeg);
            return ;
        }
        unusedPeg = 6 - fromPeg - toPeg;
        HANOITOWERS(n-1, fromPeg, unusedPeg);
        System.out.printf("Move disk from peg %d to peg %dn", fromPeg, toPeg);
        HANOITOWERS(n-1, unusedPeg, toPeg);
        return ;
 
    }
 
}

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 Javadan daha hızlı. Pythonun yorumlayıcı başlatma süresi küçük problemlerde Cden çok yavaş kalmasına sebep olurken problemler büyüdükçe C ve Python arasındaki fark azalmakta. Bilimsel hesaplamalarda Pythonun 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. Its not surprising that Python is being used in scientific calculations.


Share: FacebookGoogle+Email


comments powered by Disqus