Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有可能优化此python代码,使其执行更快?_Python_Python 2.7 - Fatal编程技术网

是否有可能优化此python代码,使其执行更快?

是否有可能优化此python代码,使其执行更快?,python,python-2.7,Python,Python 2.7,我的python代码每秒执行约110k行。我想知道是否有可能加快速度 我正在从SQL查询数据,需要将其格式化为json SQLquery= "SELECT value2 FROM mytable"; cursor.execute(SQLquery) try: ReturnedQuery = cursor.fetchall() except Exception as ex: pass if(cursor.description): #print(Retu

我的python代码每秒执行约110k行。我想知道是否有可能加快速度

我正在从SQL查询数据,需要将其格式化为json

SQLquery= "SELECT value2 FROM mytable";
cursor.execute(SQLquery)

try:
    ReturnedQuery = cursor.fetchall()    
except Exception as ex:
    pass

if(cursor.description):
        #print(ReturnedQuery)
        colTypes = cursor.description
        column_names = [column[0] for column in colTypes]
        NrOfColumns = len(column_names)
        NrOfRows = len(ReturnedQuery)
        print(NrOfRows)
        Time1 = datetime.datetime.now()
        data = []
        for row in ReturnedQuery:
            i = 0
            dataRow = collections.OrderedDict()
            for field in row:
                dataRow[column_names[i]] = field
                i = i + 1
            data.append(dataRow)
        Time2 = datetime.datetime.now()
        TimeDiff =Time2 -Time1
        print(TimeDiff)

connection.commit()
cursor.close()

从SQL查询一列将返回以下内容:
[(0.2,),(0.3,)]

我需要将其格式化为如下所示:

[OrderedDict([('value2', 0.2)]), OrderedDict([('value2', 0.3)])]
编辑: 我过滤了这个查询,以得到我想要的信息。我使用的是TimeScaleDB,所以我使用了以下查询

SELECT time_bucket('30 minutes', datetime) AS thirty_min,
AVG(value3) AS value3
FROM mytable
WHERE datetime > '2019-1-1 12:0:0.00' AND datetime < '2019-1-12 12:0:0.00'
GROUP BY thirty_min
ORDER BY thirty_min;
选择时间段(“30分钟”,日期时间)作为30分钟,
平均值(值3)作为值3
从mytable
其中datetime>'2019-1-12:0:0.00'和datetime<'2019-1-12 12:0:0.00'
三十分钟分组
30分钟前订购;

您可以使用列表理解,并尽可能快地关闭连接,以节省几个周期。因此,这可能会更有效率:

SQLquery= "SELECT value2 FROM mytable"
cursor.execute(SQLquery)

try:
    result = cursor.fetchall()    
except Exception as ex:
    pass

if cursor.description:
    column_names = [column[0] for column in cursor.description]
else:
    column_names = []
cursor.close()

if column_names:
    data = [OrderedDict(zip(column_names, row)) for row in result]

但是,如果您首先真的需要所有这些行,也许您应该看看。通常,通过在处理数据之前过滤数据,您可以以更结构化的方式保护周期。

您可以使用列表理解并尽可能快地恢复连接,以节省一些周期。因此,这可能会更有效率:

SQLquery= "SELECT value2 FROM mytable"
cursor.execute(SQLquery)

try:
    result = cursor.fetchall()    
except Exception as ex:
    pass

if cursor.description:
    column_names = [column[0] for column in cursor.description]
else:
    column_names = []
cursor.close()

if column_names:
    data = [OrderedDict(zip(column_names, row)) for row in result]

但是,如果您首先真的需要所有这些行,也许您应该看看。通常,通过在处理数据之前过滤数据,您可以以一种更结构化的方式保护循环。

假设您受到CPU的限制(因为python使用单进程),我建议您尝试使用多处理模块来分担CPU负载。 U可以将获取的列复制到列表中,并根据核心数对其进行拆分,还可以创建单独的进程来处理拆分后的数据,以利用多个核心。
将结果写入进程之间的同一共享变量时可能会出现一个问题。我使用了来自多处理模块的队列来解决这个问题。

假设您受到CPU的限制(因为python使用单进程),我建议您尝试使用多处理模块来分担CPU负载。 U可以将获取的列复制到列表中,并根据核心数对其进行拆分,还可以创建单独的进程来处理拆分后的数据,以利用多个核心。
将结果写入进程之间的同一共享变量时可能会出现一个问题。我使用了来自多处理模块的队列来解决这个问题。

python似乎正在利用所有4个线程,您使用的是哪种实现?CPython或PyPy或JPython或Ironpython。因为只有Cpython和pypy是有限的,所以我使用的是python2.7.16
https://www.python.org/downloads/release/python-2716/
python似乎正在利用所有4个线程,您使用的是哪种实现?CPython或PyPy或JPython或Ironpython。因为只有Cpython和pypy是有限的,所以我使用的是python2.7.16
https://www.python.org/downloads/release/python-2716/
您的代码看起来更整洁,这是肯定的。但是当我测试这个脚本时,它比我上面提供的脚本慢了10-20%。@tomatoeshift:你到底花了多少时间?列表理解?请注意,有一个
cursor.execute()
cursor.close()
在原始计时中没有考虑到。我只对列表理解和脚本总时间进行了计时。在这两种情况下,您的代码都比较慢
Time4=datetime.datetime.now()
data2=[]data2=[collections.OrderedDict(zip(列名,行))用于获取数据中的行]
Time5=datetime.datetime()
Diff45=Time5-Time4
您的代码看起来更整洁,这是肯定的。但是当我测试这个脚本时,它比我上面提供的脚本慢了10-20%。@tomatoeshift:你到底花了多少时间?列表理解?请注意,有一个
cursor.execute()
cursor.close()
在原始计时中没有考虑到。我只对列表理解和脚本总时间进行了计时。在这两种情况下,您的代码都比较慢
Time4=datetime.datetime.now()
data2=[]data2=[collections.OrderedDict(zip(列名,行))用于FetchedData中的行]
Time5=datetime.datetime.now()
Diff45=Time5-Time4