berniey / hanziconv

Hanzi Converter for Traditional and Simplified Chinese
Other
180 stars 43 forks source link

improve speed for converting #3

Closed noword closed 8 years ago

noword commented 8 years ago

我尝试转换一个约3M大小的文本,速度非常非常慢。 我想问题是在 hanziconv.py 第58行开始的处理上。 python的字符串,不会增长,于是不断的会有新的final创建,老的final销毁。

改成这样

        final = []
        for c in text:
            index = fromMap.find(c)
            if index != -1:
                final.append(toMap[index])
            else:
                final.append(c)
        return u"".join(final)

转换在5秒内完成。

berniey commented 8 years ago

Thanks noword! Looks like your way will be faster :). += on string is slow. Your change will be in next version

berniey commented 8 years ago

Done