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)容合作請聯(lián)系作者
  • 序言:七十年代末师倔,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌凶朗,老刑警劉巖冈闭,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抖单,死亡現(xiàn)場離奇詭異,居然都是意外死亡耍休,警方通過查閱死者的電腦和手機(jī)货矮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來喧锦,“玉大人抓督,你說我怎么就攤上這事≌缶撸” “怎么了定铜?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長揣炕。 經(jīng)常有香客問我,道長鹰溜,這世上最難降的妖魔是什么罩锐? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮仁期,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘跛蛋。我一直安慰自己赊级,他們只是感情好押框,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布橡伞。 她就那樣靜靜地躺著晋被,像睡著了一般。 火紅的嫁衣襯著肌膚如雪羡洛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天崭闲,我揣著相機(jī)與錄音威蕉,去河邊找鬼。 笑死薄翅,一個胖子當(dāng)著我的面吹牛氓奈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播舀奶,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼育勺,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了涧至?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤纺非,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后弱左,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡拆火,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年们镜,在試婚紗的時候發(fā)現(xiàn)自己被綠了乘综。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖邪意,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情萌朱,我是刑警寧澤策菜,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站又憨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏寒匙。R本人自食惡果不足惜躏将,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望会宪。 院中可真熱鬧,春花似錦掸鹅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至务甥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間敞临,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工奏黑, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留编矾,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓蹂匹,卻偏偏與公主長得像凹蜈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子仰坦,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容

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