代码顺序可以使该程序更快吗? [英] can the order of code make this program faster?

查看:68
本文介绍了代码顺序可以使该程序更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,我正在学习如何编写代码,因此从技术上讲我是新手.

我正在学习python,但我仍然很基础,我了解了if语句,并尝试将其与另一个概念(函数定义,输入,变量)混合使用,以便更广泛地了解python,我写了一些代码,但对我想做的事情没有一个明确的想法,我只是想把到目前为止学到的所有东西都混合在一起,所以我可能会做得太过头了,而且它不切实际,运行时它起作用".

我的问题不是关于如何更有效地执行或使用更少的代码,而是关于所有程序设计中的代码顺序.在这里,我将展示2种不同顺序的代码,它们使用完全相同的代码(但顺序不同)给出相同的结果.

关于(1),我在第一行定义了一个函数.
在(2)上,我在第5行使用它时更接近定义了相同的函数.

哪个更快?正在定义一个函数更接近",这对于大型程序的复杂性来说是不切实际的(但它会使速度更快),或者是在远离我需要的地方定义一个函数,使其在运行时会使较慢的程序变慢(但是更实用).

(1)

def t(n1,n2):
    v=n1-n2
    return abs(v)
a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))

c=t(a,b)

if a==b:
    print ('you are both the same age')

else:
    print('you are not the same age\nthe difference of years is %s year(s)' % c)

input()

(2)

a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))

def t(n1,n2):
    v=n1-n2
    return abs(v)

c=t(a,b)

if a==b:
    print ('you are both the same age')

else:
    print('you are not the same age\nthe difference of years is %s year(s)' % c)

input()

解决方案

函数定义必须位于函数调用上方,但是函数定义与函数调用之间的距离无关紧要(因为函数代码已经是加载).

提高程序速度的一种方法是,避免执行重复计算以产生相同的结果,方法是执行一次计算,将结果存储在变量中,并在需要结果时使用该变量.

例如,假设您的程序处理一个非常大的元组.如果len(veryLargeTuple)在代码中多次出现,Python将反复尝试确定非常大的元组的长度.这是浪费时间,因为元组是不可变的,因此其长度不会改变,并且找到非常大的元组的长度可能需要一些时间.将len(veryLargeTuple)的结果存储在变量veryLargeTupleLength中,并在需要非常大的元组的长度时使用veryLargeTupleLength会更快(查找变量的值是非常快的操作).

Hi this is my first post, I am learning how to write code so technically I am a newbie.

I am learning python I am still at the very basics, I was getting to Know the if statement and I tried to mix it with another concepts(function definition,input,variables) in order to get a wider vision of python, I wrote some code without a specific idea of what I wanted to do I just wanted to mix everything that I have learned so far so probably I over do it and its not practical, it "works" when I run it.

The question that I have its not about how to do it more efficient or with less code it is about the order of code in all programming in general. Here I'll show 2 different order of code that gives the same result with exactly the same code(but with different order).

on (1) I define a function on the first line.
on (2) I define the same function closer to when I use it on line 5.

which one is faster? is defining a function "closer" to when I need it impractical for the complexity of larger programs(but does it make it faster), or defining a function "far" from where I need it makes a larger program slower when running(but also more practical).

(1)

def t(n1,n2):
    v=n1-n2
    return abs(v)
a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))

c=t(a,b)

if a==b:
    print ('you are both the same age')

else:
    print('you are not the same age\nthe difference of years is %s year(s)' % c)

input()

(2)

a = int(input('how old are you? \n'))
b = int(input('how old is your best friend? \n'))

def t(n1,n2):
    v=n1-n2
    return abs(v)

c=t(a,b)

if a==b:
    print ('you are both the same age')

else:
    print('you are not the same age\nthe difference of years is %s year(s)' % c)

input()

解决方案

The function definition must be located above the function call, but the distance between the function definition and the function call does not matter (as the function code would already be loaded).

One way to make your program faster is to avoid repeated calculations that produce the same result, by performing the calculation once, storing the result in a variable and using that variable whenever you need the result.

For example, suppose your program handles a very large tuple. If len(veryLargeTuple) occurs many times in the code, Python will repeatedly try to determine the length of the very large tuple. This is a waste of time because a tuple is immutable, so its length would not change, and finding the length of a very large tuple may take some time. It would be faster to store the result of len(veryLargeTuple) in a variable veryLargeTupleLength and use veryLargeTupleLength whenever you need the length of the very large tuple (looking up the value of a variable is a very fast operation).

这篇关于代码顺序可以使该程序更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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