有效的字符串到十六进制功能 [英] Efficient string to hex function

查看:172
本文介绍了有效的字符串到十六进制功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在嵌入式平台上使用老版本的python( Telit平台上的Python 1.5.2+)。我有的问题是我的函数将字符串转换为十六进制。它非常缓慢。以下是函数:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b for c in s:
strHex = strHex + hexLoookup [ord(c)]

return strHex

hexLookup 是一个包含每个字符的所有十六进制表示的查找表(一个python列表)。



我愿意尝试一切(一个更紧凑的功能,一些我不知道的语言技巧)。在这里更清楚的是基准测试(该平台的分辨率为1秒):

N是要转换为十六进制的输入字符数,时间是秒。




  • N |时间(秒)

  • 50 | 1

  • 150 | 3

  • 300 | 4

  • 500 | 8

  • 1000 | 15

  • 1500 | 23

  • 2000 | 31



是的,我知道,它非常慢...但是如果我能够获得像1或2秒那样的东西,它会是一个进步。

所以任何解决方案都是受欢迎的,特别是那些了解python性能的人。



谢谢,



Iulian

PS1 :(测试提供的建议 - 保持ord调用):

  def StringToHexString(s):
hexList = []
hexListAppend = hexList.append

for c in s:
hexListAppend(hexLoookup [ord(c)])

return''.join(hexList)

使用这个函数,我获得了以下时间:1/2/3/5/12/19/27(这更清楚地表明更好)



PS2(无法解释,但速度非常快)A BIG谢谢Sven Marnach提出这个想法!!!:

  def StringToHexString(s):
return''.join(map(lambda param:hexLoookup [param],map(ord,s)))

次数:1/1/2/3/6/10/12



欢迎您提供任何其他想法/解释!

  def StringToHexString(s):
return''.join(map(lambda param:hexLoookup [param],map(ord,s)))

似乎这是最快的!谢谢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:

def StringToHexString(s):
    strHex=''

    for c in s:
        strHex = strHex + hexLoookup[ord(c)]

    return strHex

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.

  • N | Time (seconds)
  • 50 | 1
  • 150 | 3
  • 300 | 4
  • 500 | 8
  • 1000 | 15
  • 1500 | 23
  • 2000 | 31

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):

def StringToHexString(s):
    hexList=[]
    hexListAppend=hexList.append

    for c in s:
        hexListAppend(hexLoookup[ord(c)])

    return ''.join(hexList)

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 !!!:

def StringToHexString(s):
    return ''.join( map(lambda param:hexLoookup[param], map(ord,s) ) )

Times:1/1/2/3/6/10/12

Any other ideas/explanations are welcome!

解决方案

def StringToHexString(s):
    return ''.join( map(lambda param:hexLoookup[param], map(ord,s) ) )

Seems like this is the fastest! Thank you Sven Marnach!

这篇关于有效的字符串到十六进制功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆