How programming language influences our thoughts

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这世上没有谁的青春不迷茫,这世上没有谁能走出一条完全一个人的路,人生的轨迹不应该是一个人躲在角落偷偷想出来的。是的...
    流星的梦阅读 184评论 0 1
  • 1 休整了两天,整个人从原先紧绷的状态中解放了出来。一颗心闲静下来的时候,才越发感觉到孤独的如影随形。 工作了以后...
    式薇胡不归阅读 835评论 2 7
  • 今天才知道原来,教室不是休整一天,而是要休整一个星期,顿时感觉不爽,饭卡也没有办,图书馆估计也去不了,希望办卡的早...
    柠檬安然阅读 177评论 0 0
  • 好不容易打通了电话 他却忘记了南方口音 一卷绘着蛋糕的名著 在城市夜光中闪着萌 谁说她忽略了这瞬间 让我们一起举杯...
    佛系言言阅读 430评论 0 1