python 矩阵元素个数_Python的:限制在数组中元素的个数

I am using The Grinder and I have a Python script which executes some Java APIs to gather the minimum, maximum, number of executions, and total executions (the latter 2 to get the average execution times). This is done for each API (it's a multidimensional array) and each thread.

#Contents of apiTimingsList array: [min, max, number of executions, total execution time]

apiTimingsList = [[9999,0,0,0] for j in range(len(apiList))]

I am investigating some memory issues and I think that the growing size of this array might be a problem. It will grow constantly as the test runs. For example, if I have 10 APIs and I am running 900 threads, there are 9000 arrays that will keep growing as long as the test runs.

Is there a way to limit the size of these arrays, to say only keep the last x number of executions so my calculations are still valid but the arrays are not growing out of control?

解决方案

You can use collections.deque:

>>> from collections import deque

>>> d = deque(maxlen=2)

>>> d.append(3)

>>> d.append(4)

>>> d.append(5)

>>> d

deque([4, 5], maxlen=2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值