Source code For Zawgyi 2008 TO Zawgyi 2009
Python
import sys,re ENC='utf8' def zawgyi2new(d): m=open("zawgyi2new_replace.ini").read().strip().decode('utf8').replace("-",'').replace(' ','').split("\n") for x in m: x=x.split("\t") if x[0]in d: d=d.replace(x[0],x[1]) return d if __name__=='__main__': infile=sys.argv[1] d=open(infile).read().decode(ENC) d=zawgyi2new(d) outfile=infile[:infile.rfind(".")]+"_out.txt" open(outfile,"wb").write(d.encode(ENC))
PHP
$time_start = microtime(true); // get contents of a file into a string $filename = $argv[1]; $handle = fopen($filename, "r"); $d= fread($handle, filesize($filename)); fclose($handle); $myFile = "zawgyi2new.ini"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); $theData=str_replace("-","",$theData); $m=split("\n",$theData); foreach ($m as $x) { $k=split(" ",$x); if(strripos($d,$k[0])!=false) $d=str_replace($k[0],$k[1],$d); } $fp = fopen("convert_".$argv[1], 'w'); fwrite($fp, $d); fclose($fp); $time_end = microtime(true); $time = $time_end - $time_start; echo "Total ".$time." seconds";
Python က 1 second နဲ့ 3 MB ရှိတဲ့ file ကိုပြီးတယ်။ PHP ကတော့ 29 seconds လောက်ကြာတယ်။ python က ၁၅ လိုင်း။ php က ၂၃ လိုင်း။ အချိန်တွက်တာ မပါရင်တော့ လိုင်း ၁၉ ပေါ့။ ဒီထက်တိုအောင် ထပ်ရေးမယ်ဆိုရင်တော့ ၁၇ လိုင်းလောက်နဲ့ ပြီးတယ်။ ရှင်းအောင် ရေးထားလို့ပါ။ PHP နဲ့ ၁၅ လိုင်းလောက်နဲ့ရအောင် ရေးလို့ရတယ်။ ဒါပေမယ့် ဖတ်ရတာ ရှုပ်သွားမှာဆိုးလို့။
:-) Cool!
Here what your PHP code will work…
1.) Read line by line and store as String
2.) Split String into array
That’s un-necessary. You can done it in one step. And why don’t you use file_get_contents() and file_put_contentes()..?
OK, try again with this…
$d = file_get_contents($argv[1]);
$fh = fopen($myFile, ‘r’);
while($line = fgets($fh)) {
$k = split(” “,$line);
if(strripos($d, $k[1])) $d = str_replace($k[1], $k[0], $d);
}
fclose($fh);
file_put_contents(“convert_”.$argv[1], $d);
I think,
>> $d = str_replace($k[1], $k[0], $d)
should be
>>$d .= str_replace($k[1], $k[0], $d);
Cheers
its get faster? just curious :D
I interest in this issue. Please let me know if you got new bench-mark…
Thanks…
Oop! new code is slower than old. It take 35 seconds. file_get_contents() and file_put_contents is not faster than fopen() ,fread(), fwrite(). Old code take just 31 seconds. :D Python is very faster than PHP. But New code is very short and clear system.
Thank you Ko Ei Maung
@Mark
>> its get faster? just curious
It should be. At lease it should faster 2x. Tweaked two loops to one loop.
@saturngod
>> Oop! new code is slower than old. It take 35 seconds. file_get_contents() and
>> file_put_contents is not faster than fopen() ,fread(), fwrite().
Well, I’m not sure. It should faster. Technically, file_get_contents() will instantly load file content onto Memory. It should more faster. No more un-necessary Object creation. There might be another bottleneck in you code…
If you show me your detail code, I can fix it for you. I’m pretty sure there was not that much performance gap between PHP and Python…
Thanks…
FYI, check this out…
http://www.ebrueggeman.com/php_benchmarking_fopen.php