有效的字符串到十六进制功能 [英] Efficient string to hex function
问题描述
我在嵌入式平台上使用老版本的python( Telit平台上的Python 1.5.2+)。我有的问题是我的函数将字符串转换为十六进制。它非常缓慢。以下是函数: hexLookup 是一个包含每个字符的所有十六进制表示的查找表(一个python列表)。 我愿意尝试一切(一个更紧凑的功能,一些我不知道的语言技巧)。在这里更清楚的是基准测试(该平台的分辨率为1秒): N是要转换为十六进制的输入字符数,时间是秒。 是的,我知道,它非常慢...但是如果我能够获得像1或2秒那样的东西,它会是一个进步。 所以任何解决方案都是受欢迎的,特别是那些了解python性能的人。 谢谢, PS1 :(测试提供的建议 - 保持ord调用): 使用这个函数,我获得了以下时间:1/2/3/5/12/19/27(这更清楚地表明更好) PS2(无法解释,但速度非常快)A BIG谢谢Sven Marnach提出这个想法!!!: 次数:1/1/2/3/6/10/12 欢迎您提供任何其他想法/解释! 似乎这是最快的!谢谢Sven Marnach! I'm using an old version of python on an embedded platform ( Python 1.5.2+ on Telit platform ). The problem that I have is my function for converting a string to hex. It is very slow. Here is the function: hexLookup is a lookup table (a python list) containing all the hex representation of each character. I am willing to try everything (a more compact function, some language tricks I don't know about). To be more clear here are the benchmarks (resolution is 1 second on that platform): N is the number of input characters to be converted to hex and the time is in seconds. Yes, I know, it is very slow... but if I could gain something like 1 or 2 seconds it would be a progress. So any solution is welcomed, especially from people who know about python performance. Thanks, Iulian PS1: (after testing the suggestions offered - keeping the ord call): With this function I obtained the following times: 1/2/3/5/12/19/27 (which is clearly better) PS2 (can't explain but it's blazingly fast) A BIG thank you Sven Marnach for the idea !!!: Times:1/1/2/3/6/10/12 Any other ideas/explanations are welcome! Seems like this is the fastest! Thank you Sven Marnach! 这篇关于有效的字符串到十六进制功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b for c in s:
strHex = strHex + hexLoookup [ord(c)]
return strHex
Iulian
def StringToHexString(s):
hexList = []
hexListAppend = hexList.append
for c in s:
hexListAppend(hexLoookup [ord(c)])
return''.join(hexList)
def StringToHexString(s):
return''.join(map(lambda param:hexLoookup [param],map(ord,s)))
def StringToHexString(s):
return''.join(map(lambda param:hexLoookup [param],map(ord,s)))
def StringToHexString(s):
strHex=''
for c in s:
strHex = strHex + hexLoookup[ord(c)]
return strHex
def StringToHexString(s):
hexList=[]
hexListAppend=hexList.append
for c in s:
hexListAppend(hexLoookup[ord(c)])
return ''.join(hexList)
def StringToHexString(s):
return ''.join( map(lambda param:hexLoookup[param], map(ord,s) ) )
def StringToHexString(s):
return ''.join( map(lambda param:hexLoookup[param], map(ord,s) ) )