Python relevant

Good Python tutorial collection

Python 入門(mén)指南
12. 虛擬環(huán)境和包
Python教程

Python encoding and decoding

Unicode can be implemented by different character encodings. The most commonly used encodings are UTF-8, UTF-16 and the now-obsolete UCS-2. UTF-8 uses one byte for any ASCII character, all of which have the same code values in both UTF-8 and ASCII encoding, and up to four bytes for other characters. UCS-2 uses a 16-bit code unit (two 8-bit bytes) for each character but cannot encode every character in the current Unicode standard. UTF-16 extends UCS-2, using one 16-bit unit for the characters that were representable in UCS-2 and two 16-bit units (4 × 8 bits) to handle each of the additional characters.


Python process large file

Python: Fastest way to process large file

http://stackoverflow.com/questions/30294146/python-fastest-way-to-process-large-file

Question: I have multiple 3GB tab delimited files. There are 20 million rows in each file. All the rows have to be independently processed, no relation between any two rows. My question is, what will be faster A. Reading line by line using

 with open() as infile: 
    for line in infile:

Or B. Reading the file into memory in chunks and processing it, say 250 MB at a time?

It sounds like your code is I/O bound. This means that multiprocessing isn't going to help—if you spend 90% of your time reading from disk, having an extra 7 processes waiting on the next read isn't going to help anything.

And, while using a CSV reading module (whether the stdlib's csv or something like NumPy or Pandas) may be a good idea for simplicity, it's unlikely to make much different to performance.

Still, it's worth checking that you really are I/O bound, instead of just guessing. Run your program and see whether your CPU usage is close to 0% or close to 100% or a core. Do what Amadan suggested in a comment, and run your program with just pass for the processing and see whether that cuts off 5% of the time or 70%. You may even want to try comparing with a loop over os.open and os.read(1024*1024) or something and see if that's any faster.

--
Since your using Python 2.x, Python is relying on the C stdio library to guess how much to buffer at a time, so it might be worth forcing it to buffer more. The simplest way to do that is to use readlines(bufsize) for some large bufsize. (You can try different numbers and measure them to see where the peak is. In my experience, usually anything from 64K-8MB is about the same, but depending on your system that may be different—especially if you're, e.g., reading off a network filesystem with great throughput but horrible latency that swamps the throughput-vs.-latency of the actual physical drive and the caching the OS does.)

So, for example:

bufsize = 65536
with open(path) as infile: 
    while True:
        lines = infile.readlines(bufsize)
        if not lines:
            break
        for line in lines:
            process(line)

Meanwhile, assuming you're on a 64-bit system, you may want to try using mmap instead of reading the file in the first place. This certainly isn't guaranteed to be better, but it may be better, depending on your system. For example:

with open(path) as infile:
    m = mmap.mmap(infile, 0, access=mmap.ACCESS_READ)

A Python mmap is sort of a weird object—it acts like a str and like a file at the same time, so you can, e.g., manually iterate scanning for newlines, or you can call readline on it as if it were a file. Both of those will take more processing from Python than iterating the file as lines or doing batch readlines (because a loop that would be in C is now in pure Python… although maybe you can get around that with re, or with a simple Cython extension?)… but the I/O advantage of the OS knowing what you're doing with the mapping may swamp the CPU disadvantage.

Unfortunately, Python doesn't expose the madvise call that you'd use to tweak things in an attempt to optimize this in C (e.g., explicitly setting MADV_SEQUENTIAL instead of making the kernel guess, or forcing transparent huge pages)—but you can actually ctypes the function out of libc.

Excellent post: http://codereview.stackexchange.com/questions/88885/efficiently-filter-a-large-100gb-csv-file-v3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末醉旦,一起剝皮案震驚了整個(gè)濱河市姐帚,隨后出現(xiàn)的幾起案子旷痕,更是在濱河造成了極大的恐慌账千,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,324評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件秧饮,死亡現(xiàn)場(chǎng)離奇詭異于宙,居然都是意外死亡摩梧,警方通過(guò)查閱死者的電腦和手機(jī)柴灯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,356評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)卖漫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人赠群,你說(shuō)我怎么就攤上這事羊始。” “怎么了查描?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,328評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵突委,是天一觀的道長(zhǎng)柏卤。 經(jīng)常有香客問(wèn)我,道長(zhǎng)匀油,這世上最難降的妖魔是什么缘缚? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,147評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮钧唐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘匠襟。我一直安慰自己钝侠,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,160評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布酸舍。 她就那樣靜靜地躺著帅韧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪啃勉。 梳的紋絲不亂的頭發(fā)上忽舟,一...
    開(kāi)封第一講書(shū)人閱讀 51,115評(píng)論 1 296
  • 那天,我揣著相機(jī)與錄音淮阐,去河邊找鬼叮阅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛泣特,可吹牛的內(nèi)容都是我干的浩姥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,025評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼状您,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼勒叠!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起膏孟,我...
    開(kāi)封第一講書(shū)人閱讀 38,867評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤眯分,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后柒桑,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體弊决,經(jīng)...
    沈念sama閱讀 45,307評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,528評(píng)論 2 332
  • 正文 我和宋清朗相戀三年魁淳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丢氢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,688評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡先改,死狀恐怖疚察,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情仇奶,我是刑警寧澤貌嫡,帶...
    沈念sama閱讀 35,409評(píng)論 5 343
  • 正文 年R本政府宣布比驻,位于F島的核電站,受9級(jí)特大地震影響岛抄,放射性物質(zhì)發(fā)生泄漏别惦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,001評(píng)論 3 325
  • 文/蒙蒙 一夫椭、第九天 我趴在偏房一處隱蔽的房頂上張望掸掸。 院中可真熱鬧,春花似錦蹭秋、人聲如沸扰付。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,657評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)羽莺。三九已至,卻和暖如春洞豁,著一層夾襖步出監(jiān)牢的瞬間盐固,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,811評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工丈挟, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留刁卜,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,685評(píng)論 2 368
  • 正文 我出身青樓曙咽,卻偏偏與公主長(zhǎng)得像长酗,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子桐绒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,573評(píng)論 2 353

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