Python:Do(显式的)字符串参数是否会影响性能? [英] Python: Do (explicit) string parameters hurt performance?

查看:106
本文介绍了Python:Do(显式的)字符串参数是否会影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设某些函数总是得到一些它没有使用的参数。

  def someFunc(s):
#做一些不使用s的东西,例如
a = 1

现在考虑这个调用

  someFunc(未使用的字符串)

它给出一个字符串作为参数,它不是在运行时建立的,而是直接编译进二进制文件中(希望是正确的)。

问题是:以这种方式调用someFunc时,例如几千次时,对未使用的字符串的引用总是会被传递,但是会减慢程序的运行速度吗?

在我天真的想法中,我会说对未使用的字符串的引用是常量,并且在发生对someFunc的调用时在O(1)中可用。所以我会说'不,这不会损害表现'。



与以前相同的问题:我是对吗?

感谢一些: - )

解决方案

每次传入字符串开销太小,不能真正影响性能,除非它处于超级紧密的循环中。


Suppose some function that always gets some parameter s that it does not use.

def someFunc(s):
  # do something _not_ using s, for example
  a=1

now consider this call

someFunc("the unused string")

which gives a string as a parameter that is not built during runtime but compiled straight into the binary (hope thats right).

The question is: when calling someFunc this way for, say, severalthousand times the reference to "the unused string" is always passed but does that slow the program down?

in my naive thoughts i'd say the reference to "the unused string" is 'constant' and available in O(1) when a call to someFunc occurs. So i'd say 'no, that does not hurt performance'.

Same question as before: "Am I right?"

thanks for some :-)

解决方案

The string is passed (by reference) each time, but the overhead is way too tiny to really affect performance unless it's in a super-tight loop.

这篇关于Python:Do(显式的)字符串参数是否会影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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