我需要加速我的代码,我还需要使用 += i ,当然:for i in nums: sumof += i, its peivets memory block

问题描述

我尝试将我的 argmax 转换为 array.index(max(array)) 我将不胜感激任何帮助。 我需要加快我的代码。 此代码仅用于一张图片,我将在一分钟内收到大约 20 张图片。所以这就是为什么我尽量让它更快。

import time
import numpy as np

#tab = [0.061,0.001,0.022,0.002,0.015,0.021,0.066,0.005,0.053,0.018,0.011,0.024,0.052,0.077,0.010,0.045,0.017,0.016,0.007,0.071,0.167,0.004,0.009,0.014,0.059,0.008,0.009]#tabK = [0.009,0.003,0.157,0.067,0.006,0.012,0.033,0.037,0.377,0.043,0.013,0.057,0.023,0.026,0.018]CATEGORIES = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","R","S","T","U","V","W","X","Y","Z"]
#KOLEJNOSC ZNAKOW W TABLICY JEST ODWROCONA
CATEGORIES = ["0","Z"]



KR887JJ = [[0.002,0.035,0.042,0.039,0.058,0.352,0.089,0.036,0.041,0.004],[0.003,0.075,0.020,0.044,0.049,0.360,0.092,0.048,0.031,[0.002,0.000,0.025,0.627,0.040,0.053],[0.056,0.038,0.050,0.412,0.027,0.073,0.074,0.006],[0.025,0.034,0.090,0.258,0.079,0.030,0.106,0.014],[0.048,0.019,0.028,0.100,0.029,0.032,0.317,0.002],[0.013,0.164,0.060,0.383,0.063,0.012]]

[[0.004,0.054,0.051,0.056,0.389,0.070,0.005],[0.005,0.083,0.062,0.055,0.348,0.086,[0.001,0.499,0.123],[0.039,0.369,0.102,0.007],[0.031,0.365,0.099,0.010],[0.046,0.047,0.110,0.289,0.003],[0.019,0.134,0.088,0.342,0.006]]

[[0.002,0.046,0.384,0.387,0.064,0.282,0.224],[0.028,0.097,0.239,0.082,0.015],[0.037,0.250,0.021],[0.036,0.069,0.096,0.256,[0.015,0.174,0.085,0.357,0.010]]

[[0.005,0.076,0.391,0.068,[0.009,0.379,0.458,0.139],[0.042,0.111,0.235,0.084,0.023],[0.030,0.103,0.108,0.009],[0.034,0.119,0.275,[0.016,0.130,0.080,0.378,0.008]]

[[0.006,0.401,0.462,0.136],[0.004,0.422,0.065,[0.029,0.287,0.118,0.012],0.271,0.061,0.016],[0.041,0.109,0.286,[0.014,0.008]]

[[0.008,0.382,0.350,0.517,0.114],[0.026,0.121,0.011],[0.044,0.259,[0.033,0.098,0.331,[0.017,0.095,0.007]]

[[0.007,0.078,0.366,0.299,0.217],[0.021,0.234,0.020],0.269,0.018],0.140,[0.018,0.186,0.107,0.300,0.008]]

start = time.time()

for sing in KR887JJ: 
    print(CATEGORIES[np.argmax(sing)])

    #print(CATEGORIES[sing.index(max(sing))])




def list_mean(nums):
    sumof = 0
    num_of = len(nums)
    mean = 0
    for i in nums:
        sumof += i 
    mean = sumof / num_of
    return float(mean)


end = time.time()

print(f"Runtime of the program is {end - start}")

我也 100% 需要这部分,因为我在 nums:
总和 += i。像这样的迭代不会占用大量内存,因为它不断地计数而不是那些具有大块内存和平的迭代。 提前致谢!

解决方法

np.argmax 并不比 array.index(np.max(array)) 慢,但是 list_mean 可以更有效率:如果你想要一个完整的 python 实现,你可以使用

sum(nums)/len(nums)

甚至更高效:

np.mean(nums)

(你已经使用过数组)

,

或多或少: 导入时间 将 numpy 导入为 np

#KOLEJNOSC ZNAKOW W TABLICY JEST ODWROCONA 类别 = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N"," O","P","R","S","T","U","V","W","X","Y","Z"]

KR887JJ = [[0.002,0.006,0.004,0.045,0.002,0.017,0.077,0.001,0.035,0.042,0.005,0.0.0.0.039,0.0.0.0.0.5,0.007,0.009,0.089,0.036,0.053,0.041,0.004],[0.003,0.0.0.0.04],0.0.0.0.0.04,0.04,0.06.0.0.0.0.0.07,0.033,0.003,0.008,0.049,0.360,0.021,0.0008.0.0.04,0.0007 [0.002,0.000,0.025,0.012,0.627,0.022,0.011,0.040,0.015,0.053],[0.056,0.023,0.038,0.0.0.0.04,0.056,0.0.0.0.0.07,0.073,0.024,0.013,0.027,0.074,0.02.,0.034,0.018,0.090,0.258,0.026,0.016,0.079,0.0010,0.0180,0.0103.03,0.106,0.014],[0.048,0.019,0.0.0.0.014,0.0.0.0.0.02.7,0.00.02,0.031,0.100,0.029,0.032,0.0.0.0.017,0.0.0.0.07 ,0.002,0.164,0.060,0.028,0.383,0.063,0.010,0.014,0.048,0.012]] [[0.004,0.054,0.051,0.044,0.0002.0.001,0.0002.0.0.00.05.3.0.0.005.0.005.3 0.009,0.070,0.005],[0.005,0.0.005,0.0.002.0.06.002.05.002 0.039,0.055,0.348,0.020,10.0.2 0.024,0.499,0.009, 0.007,0.030,0.123],[0.039,0.0.05.0.04,0.0.05.04,0.0.05.05 0.100,0.102,0.0.3,0.0.5 0.017,0.075,0.365,0.05,0.0.02,0.0.05,0.02,0.0. 0.001,0.099,0.010],[0.046,0.002.0.031,0.007.003.0.07. 0.048,0.110,0.059,0.289,0.0.0.3,0.03,0.0.0.0.003,0.0.0. 0.000,0.134,0.088,0.037,0.342,0.009, 0.005,0.006]] [[0.002,0.046,0.00.0.004,0.00.5 0.006,0.066,[0.002,0.039,0.0.009.0.009.04 0.026,0.387,0.0.0.3 0.014,0.282,0.010, 0.016,0.043,0.224],[0.028,0.097,0.009.0.004,0.009.04,0.009.004 0.082,0.3,5.0.3,0.0.5,0.0.3 0.022,0.057,0.250,0.07,00.2. 0.002,0.092,0.021],[0.036,0.00104,0.0104,0.0104 0.057,0.096,0.256,0.0.3 0.000,0.174,0.085,0.357,0.010, 0.004,0.010]] [[0.005,0.076,0.050,0.001.0.001,0.001.0.001.0.001.0.001.0.001.0.001 0.012,0.068,0.006],[0.009,0.064,0.0.002.3,0.002.002 0.069,0.379,0.0.0.0.03,1,0.0.0.03,0.0.7 0.014,0.458,0.052,0.008, 0.009,0.139],[0.042,0.111,0.030.01,0.03.01,0.03.0. 0.039,0.084,0.0.3 0.020,0.103,0.108,00.0.03,0.0.0.020,0.009],[0.034,0.0.05.0.05.0.05.0.05.05.05 0.050,0.119,0.275,0.0.0.003,0.0.5 0.000,0.130,0.080,0.378,0.012, 0.004,0.008]] [[0.006,0.0007,0.00.0.04,0.00.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.00.0.0.0.0.007 0.013,[0.001,0.0.01.0.04,0.04 0.006,6,0.0.3 0.007,0.062,0.422,0.010,的0.059,0.118,0.4,0.0.4,0.0.4 0.015,0.065,0.271,0.06,03,0.0.0.016,0.0. 0.005,0.061,0.016],[0.041,0.060.04,0.06.004 0.051,0.109,0.067,0.286,0.01,0.0.0.3 0.000,0.377,0.009, 0.004,0.008]] [[0.008,0.00.0.007,0.001.0.007.0. 0.015,0.0.001.3,0.0.001.005.001.001.001.001.001.001.005 0.030,0.350,0.0.0.7,0.0.3 0.015,0.517,0.006, 0.009,0.114],[0.026,0.0.00.04,0.0.00.07 0.067,0.121,0.7,0.0.7 0.016,0.259,04,0.0.0.07,0.0. 0.002,0.086,0.011],[0.033,0.003.0.03.04,0.003.0.03.0.03. 0.056,0.098,0.069,0.331,0.0.0.0.002,0.0.0.2,0.0. 0.000,0.095,0.008, 0.005,0.007]] [[0.007,0.082,0.00.0.009.0.0.0.0.009.0.0.001,0.009.0.0.0.0.009.0.0.001,0.009.0 0.016,[0.004,0.078,0.0.005.04,0.005.005 0.052,0.366,0.0.2 0.013,0.299,0.008,的0.052,0.0.014,0.3 0.020,0.269,0.018],0.0.003.0.04,0.003.0.04 0.050,0.140,0.239,0.186,0.107,0.300,0.010, 0.005,0.008]]

开始 = time.time()

在 KR887JJ 中演唱: 打印(类别[sing.index(max(sing))])

def list_mean(nums):

sumof,len = 0,0
for i in nums:      
    sumof += i
#len += 1
return sumof / len

end = time.time()

print(f"程序的运行时间是{end - start}")