I'm working in the Google App Engine environment and programming in Python. I am creating a function that essentially generates a random number/letter string and then stores to the memcache.

我正在使用Google App Engine环境并使用Python进行编程。我正在创建一个函数,它基本上生成一个随机数字/字母字符串,然后存储到memcache。

def generate_random_string():
# return a random 6-digit long string

def check_and_store_to_memcache():
    randomstring = generate_random_string()
    #check against memcache
    #if ok, then store key value with another value
    #if not ok, run generate_random_string() again and check again.

Does creating two functions instead of just one big one affect performance? I prefer two, as it better matches how I think, but don't mind combining them if that's "best practice".

创建两个函数而不是一个大函数会影响性能吗?我更喜欢两个,因为它更符合我的想法,但如果这是“最佳实践”,请不要介意将它们结合起来。

4 个解决方案

#1


Focus on being able to read and easily understand your code.

专注于能够阅读并轻松理解您的代码。

Once you've done this, if you have a performance problem, then look into what might be causing it.

完成此操作后,如果遇到性能问题,请查看可能导致性能问题的原因。

Most languages, python included, tend to have fairly low overhead for making method calls. Putting this code into a single function is not going to (dramatically) change the performance metrics - I'd guess that your random number generation will probably be the bulk of the time, not having 2 functions.

包含python的大多数语言在进行方法调用时往往具有相当低的开销。将此代码放入单个函数不会(显着)改变性能指标 - 我猜你的随机数生成可能是大部分时间,没有2个函数。

That being said, splitting functions does have a (very, very minor) impact on performance. However, I'd think of it this way - it may take you from going 80 mph on the highway to 79.99mph (which you'll never really notice). The important things to watch for are avoiding stoplights and traffic jams, since they're going to make you have to stop altogether...

话虽这么说,拆分功能确实会对性能产生(非常非常小的)影响。但是,我会这么想的 - 它可能会让你从高速公路上的80英里/小时到79.99英里/小时(你永远不会注意到)。需要注意的重要事项是避免红绿灯和交通拥堵,因为它们会让你不得不完全停下来......

更多相关文章

  1. python cookboo 文件与IO 函数
  2. 在Python中强制使用函数参数类型?
  3. Python(名称空间、函数嵌套、函数对象)
  4. 数据挖掘(三)分类模型的描述与性能评估,以决策树为例
  5. python中函数参数传递的几种方法
  6. python函数篇0-2
  7. Python的范围函数如何工作?
  8. Python日期和时间函数
  9. Python入门:内置函数

随机推荐

  1. android坐标系相关知识点
  2. Android技术人才前途无量
  3. 7GB! | 高焕堂Android从程序员到架构师之
  4. 深入理解Android音频框架AudioTrack到Aud
  5. Android(Java):对应用进行单元测试
  6. 移动互联网应用技术架构简介-Android
  7. android之Intent基本使用
  8. Linux与Android
  9. Android中Activity的初步接触(一)
  10. 彻底解决Android GPS没法定位这一顽固问