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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這世上沒(méi)有誰(shuí)的青春不迷茫,這世上沒(méi)有誰(shuí)能走出一條完全一個(gè)人的路,人生的軌跡不應(yīng)該是一個(gè)人躲在角落偷偷想出來(lái)的。是的...
    流星的夢(mèng)閱讀 207評(píng)論 0 1
  • 1 休整了兩天,整個(gè)人從原先緊繃的狀態(tài)中解放了出來(lái)。一顆心閑靜下來(lái)的時(shí)候,才越發(fā)感覺(jué)到孤獨(dú)的如影隨形。 工作了以后...
    式薇胡不歸閱讀 864評(píng)論 2 7
  • 今天才知道原來(lái),教室不是休整一天,而是要休整一個(gè)星期,頓時(shí)感覺(jué)不爽,飯卡也沒(méi)有辦,圖書館估計(jì)也去不了,希望辦卡的早...
    檸檬安然閱讀 184評(píng)論 0 0
  • 好不容易打通了電話 他卻忘記了南方口音 一卷繪著蛋糕的名著 在城市夜光中閃著萌 誰(shuí)說(shuō)她忽略了這瞬間 讓我們一起舉杯...
    佛系言言閱讀 488評(píng)論 0 1

友情鏈接更多精彩內(nèi)容