【问题标题】:Measure the time interval between the start of execution of a python program and the end of its execution测量python程序开始执行和执行结束之间的时间间隔
【发布时间】:2020-04-28 08:29:45
【问题描述】:

我一直在尝试将代码添加到下面的脚本 pong.py 中,以测量屏幕保持打开和关闭的时间。我在运行 pygame 时遇到问题,所以我想找到一种方法来计算这个时间间隔而不使用 pygame。 我在 stack.overflow 中查看了几个问题,但没有一个真正说明我的 python 屏幕程序保持打开状态的时间。有些答案有 pygame,我正在避免使用 pygame 来获取错误消息。

pong.py:

    # Simple Pong in Python 3 for Beginners
# By @TokyoEdTech
# Part 5: Colliding with the paddles

import turtle

wn = turtle.Screen()
wn.title("Pong by @TokyoEdTech")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 2
ball.dy = -2

# Function
def paddle_a_up():
    y = paddle_a.ycor()
    y += 20
    paddle_a.sety(y)

def paddle_a_down():
    y = paddle_a.ycor()
    y -= 20
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor()
    y += 20
    paddle_b.sety(y)

def paddle_b_down():
    y = paddle_b.ycor()
    y -= 20
    paddle_b.sety(y)

# Keyboard binding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")

# Main game loop
while True:
    wn.update() 

    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Border checking
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1

    if ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1

    if ball.xcor() > 390:
        ball.goto(0, 0)
        ball.dx *= -1

    if ball.xcor() < -390:
        ball.goto(0, 0)
        ball.dx *= -1

    # Paddle and ball collisions
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):
        ball.setx(340)
        ball.dx *= -1

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):
        ball.setx(-340)
        ball.dx *= -1

更新

示例

我的意思是当我打开屏幕时,例如,如果这是一个游戏,我应该在我保持程序打开时计算时间。

我想要一种让它在脚本中工作的方法,但我也对命令行感兴趣。

【问题讨论】:

    标签: python performance timer clock execution


    【解决方案1】:

    您好,

    有很多方法可以知道 Python 中程序的执行时间。

    我会提到两种简单且常用的方法:-

    • 使用时间库

    time是python中的一个库,用于操纵和玩弄时间。

    import time
    start = time.time()
    ...
    ...
    print(time.time() - start)
    
    • 在命令行

    如果你从命令行执行程序,你可以试试这个。

    $ time python3 code.py
    

    这一行提供了系统运行代码所用时间的基本信息。

    【讨论】:

    • 只是出于好奇,这个命令行也适用于 windows 吗?
    • @High-Octane 我已经更新了这个问题,现在使用time python3 code.py 正在工作,尽管使用import time start = time.time() ... ... print(time.time() - start)see output in my terminal imgur.com/FJtkTwn.
    • @NitinSai 即我已经更新了问题,现在使用时间 python3 code.py 正在工作,尽管使用import time start = time.time() ... ... print(time.time() - start) 请参阅我的终端 imgur.com/FJtkTwn 中的输出
    【解决方案2】:

    你试过这个好先生吗?

    import turtle 
    import time
    start_time = time.time()
    #Set up the screen 
    try:
        wn = turtle.Screen() 
        wn.bgcolor("black") 
        wn.title("Test") 
        wn.bgpic("grid.png")
        print("Execution time :  %s seconds " % (str(time.time() - start_time)))
    except Exception as e:
        print("Program exited by error",e)
        print("Execution time : %s seconds " % (str(time.time() - start_time)))
    
    

    【讨论】:

    • 谢谢XD!原谅我,我表达得很糟糕,我的意思是当我打开屏幕时,例如,如果它是一个游戏,我应该在我保持程序打开时计算时间。
    • 我收到以下错误:File "&lt;string&gt;", line 103 print("Execution time : %s seconds " % (time.time() - start_time)) ^ SyntaxError: invalid syntax
    • 你可以尝试在外面使用字符串吗?也更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多