想知道python的性能。我生成了以下代码来检查raspberry-pi 3的性能.python版本是2.7.9。
pslibs / psDebug.py文件
import sys
import time
from datetime import datetime
class CliColor :
YELLOW ='\x1b[00;033;033m'
GREEN = '\x1b[00;032;032m'
RED = '\x1b[00;031;031m'
NONE = '\x1b[00m'
class PSDebug :
@staticmethod
def debug(msg, color=CliColor.NONE) :
if color == CliColor.NONE :
print >> sys.stderr, "%s @ %s" % (datetime.now().strftime("%H:%M:%S.%f"), msg)
else :
print >> sys.stderr, "%s%s @ %s%s" % (color, datetime.now().strftime("%H:%M:%S.%f"), msg, CliColor.NONE)
def DBG(arg, color=CliColor.NONE) :
PSDebug.debug(arg, color)
sys.stderr.flush()
test.py文件
#!/usr/bin/env python
import sys
from libps.psDebug import DBG
count = 0
while count <= 10000 :
DBG('Count : %d' % count)
count += 1
第一个,在终端中执行'time ./test.py'。它工作正常,并生成了libps / psDebug.pyc。
第二个,删除了libps / psDebug.py并使用libps / psDebug.pyc执行'time ./test.py'。
第三,恢复libps / psDebug.py。运行'pyinstaller test.py'并执行'time ./test'。
每个结果如下。
real 0m11.946s
user 0m10.130s
sys 0m1.550s
real 0m26.482s
user 0m24.340s
sys 0m1.820s
real 0m20.362s
user 0m18.820s
sys 0m1.010s
我的观点中的预测似乎反向更快。但脚本.py文件更快。当我使用strace实用程序进行调试时,唯一不同的是“统计系统调用错误”。
首先,'时间./test.py'
stat64("/work/temp/libps/psDebug.py", {st_mode=S_IFREG|0644, st_size=936, ...}) = 0
stat64("/work/temp/libps/psDebug.py", {st_mode=S_IFREG|0644, st_size=936, ...}) = 0
stat64("/work/temp/libps/psDebug.py", {st_mode=S_IFREG|0644, st_size=936, ...}) = 0
stat64("/work/temp/libps/psDebug.py", {st_mode=S_IFREG|0644, st_size=936, ...}) = 0
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
getcwd("/work/temp", 1024) = 11
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
getcwd("/work/temp", 1024) = 11
gettimeofday({1463756954, 935083}, NULL) = 0
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=557, ...}) = 0
write(2, "00:09:14.935083 @ Count : 9999", 30) = 30
write(2, "\n", 1) = 1
其次,'时间./test.py'
stat64("/work/temp/libps/psDebug.py", 0x7e979a40) = -1 ENOENT (No such file or directory)
stat64("/work/temp/libps/psDebug.py", 0x7e979980) = -1 ENOENT (No such file or directory)
stat64("/work/temp/libps/psDebug.py", 0x7e979a40) = -1 ENOENT (No such file or directory)
stat64("/work/temp/libps/psDebug.py", 0x7e979980) = -1 ENOENT (No such file or directory)
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
getcwd("/work/temp", 1024) = 11
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
stat64("./test.py", {st_mode=S_IFREG|0755, st_size=185, ...}) = 0
getcwd("/work/temp", 1024) = 11
gettimeofday({1463756850, 285897}, NULL) = 0
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=557, ...}) = 0
write(2, "00:07:30.285897 @ Count : 9999", 30) = 30
write(2, "\n", 1) = 1
第三,'时间./test'
stat64("libps/psDebug.py", 0x7eac9a10) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
stat64("libps/psDebug.py", 0x7eac9928) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
getcwd("/work/temp/dist/test", 1024) = 21
stat64("libps/psDebug.py", 0x7eac9858) = -1 ENOENT (No such file or directory)
stat64("libps/psDebug.py", 0x7eac9a10) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
stat64("libps/psDebug.py", 0x7eac9928) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
getcwd("/work/temp/dist/test", 1024) = 21
stat64("libps/psDebug.py", 0x7eac9858) = -1 ENOENT (No such file or directory)
stat64("test.py", 0x7eac9a10) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
stat64("test.py", 0x7eac9928) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
stat64("test.py", 0x7eac9cb0) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
stat64("test.py", 0x7eac9bc8) = -1 ENOENT (No such file or directory)
getcwd("/work/temp/dist/test", 1024) = 21
gettimeofday({1463756115, 1765}, NULL) = 0
stat64("/etc/localtime", {st_mode=S_IFREG|0644, st_size=557, ...}) = 0
write(2, "23:55:15.001765 @ Count : 6912", 30) = 30
write(2, "\n", 1) = 1
请建议如何改善第二种和第三种方法的性能。