高效的字符串转十六进制函数

发布于 2024-12-12 12:29:21 字数 1312 浏览 0 评论 0原文

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

def StringToHexString(s):
    strHex=''

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

    return strHex

hexLookup 是一个查找表(Python 列表),其中包含每个字符的所有十六进制表示形式。

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

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

  • 尼 |时间(秒)
  • 50 | 1
  • 150 | 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(无法解释,但速度非常快)非常感谢 Sven Marnach 的想法!!!:

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

时间:1/1/2/3/6/10/12

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贪了杯 2024-12-19 12:29:21

让您的 hexLoookup 成为由字符本身索引的字典,这样您就不必每次都调用 ord

另外,不要连接来构建字符串——这在过去很慢。请改为在列表上使用join

from string import join
def StringToHexString(s):
    strHex = []

    for c in s:
        strHex.append(hexLoookup[c])

    return join(strHex, '')

Make your hexLoookup a dictionary indexed by the characters themselves, so you don't have to call ord each time.

Also, don't concatenate to build strings – that used to be slow. Use join on a list instead.

from string import join
def StringToHexString(s):
    strHex = []

    for c in s:
        strHex.append(hexLoookup[c])

    return join(strHex, '')
长不大的小祸害 2024-12-19 12:29:21

基于 Petr Viktorin 的回答,您可以通过避免全局变量和属性查找有利于局部变量查找。局部变量经过优化,以避免每次访问时都进行字典查找。 (它们并不总是如此,我只是仔细检查了这一优化是否已经在 1999 年发布的 1.5.2 中到位。)

from string import join
def StringToHexString(s):
    strHex = []
    strHexappend = strHex.append
    _hexLookup = hexLoookup
    for c in s:
        strHexappend(_hexLoookup[c])
    return join(strHex, '')

Building on Petr Viktorin's answer, you could further improve the performance by avoiding global vairable and attribute look-ups in favour of local variable look-ups. Local variables are optimized to avoid a dictionary look-up on each access. (They haven't always been, by I just double-checked this optimization was already in place in 1.5.2, released in 1999.)

from string import join
def StringToHexString(s):
    strHex = []
    strHexappend = strHex.append
    _hexLookup = hexLoookup
    for c in s:
        strHexappend(_hexLoookup[c])
    return join(strHex, '')
一抹苦笑 2024-12-19 12:29:21

使用 + 运算符不断重新分配和添加字符串非常慢。我猜想 Python 1.5.2 还没有对此进行优化。因此使用 string.join()< /code>会更好。

尝试

import string
def StringToHexString(s):
    listhex = []
    for c in s:
        listhex.append(hexLookup[ord(c)])
    return string.join(listhex, '')

看看是否更快。

Constantly reassigning and adding strings together using the + operator is very slow. I guess that Python 1.5.2 isn't yet optimizing for this. So using string.join() would be preferable.

Try

import string
def StringToHexString(s):
    listhex = []
    for c in s:
        listhex.append(hexLookup[ord(c)])
    return string.join(listhex, '')

and see if that is any faster.

无力看清 2024-12-19 12:29:21

尝试:

from string import join

def StringToHexString(s):
    charlist = []

    for c in s:
        charlist.append(hexLoookup[ord(c)])

    return join(charlist, '')

每个字符串添加所需的时间与字符串的长度成正比,因此 join 也将花费与整个字符串的长度成正比的时间,但你只需要做一次< /em>。

您还可以将 hexLookup 设为将字符映射到十六进制值的 dict,这样您就不必为每个字符调用 ord。这是一个微观优化,所以可能不会很重要。

Try:

from string import join

def StringToHexString(s):
    charlist = []

    for c in s:
        charlist.append(hexLoookup[ord(c)])

    return join(charlist, '')

Each string addition takes time proportional to the length of the string so, while join will also take time proportional to the length of the entire string, but you only have to do it once.

You could also make hexLookup a dict mapping characters to hex values, so you don't have to call ord for every character. It's a micro-optimization, so probably won't be significant.

善良天后 2024-12-19 12:29:21
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) ) )

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文