Created
March 13, 2016 11:28
-
-
Save HelloZeroNet/f684d2f1eb7807a124b3 to your computer and use it in GitHub Desktop.
bench_translate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, time | |
data = open("all2.js").read() | |
translate = { | |
"Please, choose your account before creating a topic.": "TRANS1", | |
"Please, choose your account before upvoting.": "TRANS2", | |
"Comments requires at least ZeroNet 0.3.0 Please upgade!": "TRANS3", | |
"Every new user has limited space to store comments, topics and votes.\\n": "TRAN4", | |
"This indicator shows your used/total allowed KBytes.\\n": "TRANS5", | |
"The site admin can increase it if you about to run out of it.": "TRANS6", | |
"ZeroTalk requires ZeroNet 0.3.1, please update!": "TRANS7", | |
"Main": "TRANS8", | |
"Newest topics": "TRANS9", | |
" minutes ago": "TRANS10", | |
" hours ago": "TRANS11", | |
" days ago": "TRANS12", | |
"""Are you sure you sure to delete this " + object_type + "?", "Delete""": "TRANS13", | |
"""(Done in " + ms + "ms)""": "TRANS14" | |
} | |
print "100x Using re..." | |
s = time.time() | |
def replacer(match): | |
return match.group(0)[0]+translate[match.group(1)]+match.group(0)[-1] | |
for i in range(100): | |
patterns = [] | |
for key, val in translate.items(): | |
patterns.append(re.escape(key)) | |
pattern = "[>\"]("+"|".join(patterns)+")[<\"]" | |
data_re = re.sub(pattern, replacer, data) | |
print time.time()-s | |
open("all2-re.js", "w").write(data_re) | |
print "100x Using re + split..." | |
s = time.time() | |
for i in range(100): | |
patterns = [] | |
for key, val in translate.items(): | |
patterns.append(re.escape(key)) | |
parts = data.split("/* ----") | |
parts_out = [] | |
for part in parts: | |
if "/js/lib" in part[0:200]: | |
parts_out.append(part) | |
else: | |
pattern = "[>\"]("+"|".join(patterns)+")[<\"]" | |
parts_out.append(re.sub(pattern, replacer, part)) | |
data_re_split = "/* ----".join(parts_out) | |
print time.time()-s | |
open("all2-re-split.js", "w").write(data_re_split) | |
print "100x Using re + split double keys..." | |
s = time.time() | |
for i in range(100): | |
patterns = [] | |
for key, val in translate.items(): | |
patterns.append(re.escape(key)) | |
for key, val in translate.items(): | |
patterns.append(re.escape(key)) | |
parts = data.split("/* ----") | |
parts_out = [] | |
for part in parts: | |
if "/js/lib" in part[0:200]: | |
parts_out.append(part) | |
else: | |
pattern = "[>\"]("+"|".join(patterns)+")[<\"]" | |
parts_out.append(re.sub(pattern, replacer, part)) | |
data_re_split = "/* ----".join(parts_out) | |
print time.time()-s | |
open("all2-re-split.js", "w").write(data_re_split) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
100x Using re...
0.482000112534
100x Using re + split...
0.263999938965
100x Using re + split double keys...
0.31200003624