【问题标题】:Getting the time of compilation using Python in Ubuntu在 Ubuntu 中使用 Python 获取编译时间
【发布时间】:2018-04-09 17:27:52
【问题描述】:

我正在 python 中实现多线程代码。此示例每 2 秒打印一条消息。与 Windows 10 中 Pycharm 的结果相比,Ubuntu 中终端的输出不合逻辑。我认为 time 模块应该在两种环境中都可以工作。我不知道为什么它在 Linux 中不起作用。 另外,有没有其他类似time的模块可以使用?

感谢您的帮助

代码:

from threading import Thread
from time import sleep
from time import clock as clk
## To create a thread in Python you'll want to make your class work as a thread.
## For this, you should subclass your class from the Thread class
s2 = 0
class Book(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.message = ("Hello Parallel Python!!\n")

    def print_message(self):
        print (self.message)

##The run method prints the message
    def run(self):
        print ("Thread Starting")
        x=0
        while (x < 10):
            self.print_message()
            sleep(2)
            print "Time is : ", clk()
            x += 1
        print ("Thread Ended\n")
        s2 = clk()
        print "2st s2 is: ", s2

#start the main process
print ("Process Started")

# create an instance of the HelloWorld class
hello = Book()

# print the message...starting the thread
s1 = clk()
hello.start()

#end the main process
print ("Process Ended\n")
print "1st s2 is: ", s2

在 Windows 10 中使用 Pycharm 的输出是:

Pycharm result

在 Ubuntu 16.04 LTS 中使用 gedit 的输出是:

terminal result

【问题讨论】:

标签: python multithreading python-2.7 time ubuntu-16.04


【解决方案1】:

是的,你得到了timeit,它是一个可用于计算执行时间的模块。

您可以尝试按照更新的代码进行检查。

import timeit
from threading import Thread
from time import sleep
from time import clock as clk
## To create a thread in Python you'll want to make your class work as a thread.
## For this, you should subclass your class from the Thread class
s2 = 0
class Book(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.message = ("Hello Parallel Python!!\n")

    def print_message(self):
        print (self.message)

##The run method prints the message
    def run(self):
        timeAtStart = timeit.default_timer()
        start_time = timeit.default_timer()
        print ("Thread Starting")
        x=0
        while (x < 10):
            self.print_message()
            sleep(2)
            x += 1
            print(timeit.default_timer() - start_time)
            start_time = timeit.default_timer()
        print ("Thread Ended\n")

        print("2st s2 is: ", timeit.default_timer()-timeAtStart)

#start the main process
print ("Process Started")

# create an instance of the HelloWorld class
hello = Book()

# print the message...starting the thread
hello.start()

以下是一些结果:

对于 Windows:

Hello Parallel Python!!

2.0153833086419755
Hello Parallel Python!!

2.0137947654320985
Hello Parallel Python!!

2.0127004444444445
Hello Parallel Python!!

2.0082500740740743
Hello Parallel Python!!

2.0016956049382717
Hello Parallel Python!!

2.012038716049382
Hello Parallel Python!!

2.014874469135803
Hello Parallel Python!!

2.0006115555555546
Hello Parallel Python!!

适用于 Linux(Ubuntu)

Hello Parallel Python!!

2.00243210793
Hello Parallel Python!!

2.00301098824
Hello Parallel Python!!

2.00235509872
Hello Parallel Python!!

2.00234389305
Hello Parallel Python!!

2.00045418739
Hello Parallel Python!!

2.00276589394
Hello Parallel Python!!

2.00276899338
Hello Parallel Python!!

【讨论】:

    【解决方案2】:

    time.clock 在不同的系统上测量不同的东西。在您的 Linux 系统上,它不计算睡眠时间。

    用于测量已用实时的最佳函数取决于您的操作系统和 Python 版本。 timeit.default_timer 是操作系统和 Python 版本独立性的良好默认值。

    【讨论】:

    • 感谢您的解释。竖起大拇指
    猜你喜欢
    • 2019-10-13
    • 2012-04-13
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多