美文网首页
How programming language influen

How programming language influen

作者: yfwz100 | 来源:发表于2015-06-24 14:33 被阅读17次

    It's the topic when I occasionally discuss a implementation detail of an algorithm last week with Wang Wei. He's now busy preparing his thesis and graduate presentation the end of June. We discuss much about the algorithm and the experiments. I said that the experiment part, which requires the implementation of the previous methods or the well-known methods, is the most contraversal part of a paper. But he thought the effect will not be so large to mention and ignore it.

    Back at home, I thought of the discussion more closely. As I implemented the same agorithm in Java and Python in my previous experiment(AI homework in Python and the corresponding Android game in Java). I found that the 2 languages has different point of view towards data. And the implementation detail in the control procedure will influences the efficiency of the algorith.

    Think of the common loop coded in Python:

    for i in range(1000):
        do_something_with(i) 
    

    And

    for i in xrange(1000): 
        do_something_with(i) 
    

    The code above are all standard loop code in Python (This means that almost all people loop in this way and many tutorials recommend this way) but the two pieces of code will have very different performance regarding the size of the loop: the range(...) function in Python 2 generates a list and when the size is large it costs much to allocatememory and do the loop while the xrange(...) function just generates a iterator and will not pre-allocate any structure of a looping list. If you don't know the detail of the implementation, you will probably get a poor code of the well-designed algorithm.

    Furthermore, the phenomenon is not rare in other programming language. Java 8 introduces a new concept of collection manipulation: the streaming API. It's a well-designed API in regarding of the functional programming and it will change the thought about collection iteration.

    The common loop coded in Java:

    collection = ...;// define a collection
    for (int i=0; i < collection.size(); i++) {
        doSomethingWith(collection.get(i)); 
    } 
    

    and the streaminng API:

    collection.forEach(e -> {
        doSomethingWith(e); 
    });
    

    It seems trivial in the first sight of the code. In fact, in a plain reference implementation of forEach(...) method is a plain loop with callback in each iteration. However, the stream API hides the implementation details and introduces the concept of parallel stream.

    In Java 8, the code could be written as

    collection.parallel().forEach(e -> { 
        doSomethingWith(e); 
    });
    

    It has totally different performance of algorithm. In each orderless iteration, the above code could be executed in different thread backed by the parallel stream. It's easy to imagine the performance difference when the collection is large.

    Therefore, choosing the right language to use is still a challenge in the research field or the industry field. The implementation detail of the language may influence your code in an unnoticable way.

    相关文章

      网友评论

          本文标题:How programming language influen

          本文链接:https://www.haomeiwen.com/subject/rwnuxttx.html