[PYTHON] I measured 6 methods to get the index of the maximum value (minimum value) of the list

As mentioned above

Get the index of the maximum value (minimum value) of the list

I measured the 5 methods described in Mr. Uzen's article above. For the measurement code, I used Measure and display the processing time.

Postscript

I also added the processing with numpy that was pointed out in the comment. Except for the conversion cost of array → numpy, numpy was overwhelmingly fast. It was too early, so I increased the number of elements in the target array by an order of magnitude.

result

Execution result, implementation code

Output result (part)


elapsed_time:0.027699708938598633[sec] 
elapsed_time:0.15381669998168945[sec] 
elapsed_time:0.1455528736114502[sec] 
elapsed_time:0.09561371803283691[sec] 
elapsed_time:0.07018375396728516[sec] 
elapsed_time:0.08819770812988281[sec] 
elapsed_time:0.0008273124694824219[sec] 
[244347, 674860, 655894, 255695, 678218, 531167, 17071, 288341, 429050, 146444, 291065, 27607, 556887, 298302, 515557, 271078, 883608, 9098, 752318, 276251, 349950, 133988, 955108, 322233, 613472, 844009, 298534, 485112, 106167, 516815, 865031, 286926, 413458, 883781, 910152, 942716, 913758, 853043, 377110, 527816, 478540, 920741, 770539, 169002, 820915, 231827, 423725, 673925, 182482, 14842, 515316, 999412, 152994, 986206, 851899, 403318, 137434, 163888, 46630, 347440, 920191, 506127, 935566, 981816, 353811, 196066, 106365, 739729, 62010, 426593, 

Implementation code


import time
import random
import numpy as np

list_name = []
max_value = 1000000

for i in range(max_value ):
    list_name.append(random.randint(1,max_value))

start = time.time()
list_name.index(max(list_name))
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

start = time.time()
max(enumerate(list_name), key=lambda x: x[1])[0]
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

start = time.time()
max(range(len(list_name)), key=lambda i: list_name[i])
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

import operator

start = time.time()
max(enumerate(list_name), key=operator.itemgetter(1))[0]
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

start = time.time()
max(zip(list_name, range(len(list_name))))[1]
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

# numpy (array → numpy conversion processing included)
start = time.time()
np_list_name = np.array(list_name)
np_list_name.argmax()
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

# numpy (array → numpy without conversion processing)
np_list_name2 = np.array(list_name)
start = time.time()
np_list_name2.argmax()
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

print(list_name)

Serpentine

I tried to tell in the comments on the blog, but I couldn't find the comment section. Deliver this feeling.

~~ And even the fastest code can't solve a certain problem ... If you know a better way, please let me know. ~~ → Solved!

Recommended Posts

I measured 6 methods to get the index of the maximum value (minimum value) of the list
I tried to get the index of the list using the enumerate function
Get the value of a specific key up to the specified index in the dictionary list in Python
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
Try to get the function list of Python> os package
I tried to get the location information of Odakyu Bus
I want to get the operation information of yahoo route
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
I tried to fight the Local Minimum of Goldstein-Price Function
Keras I want to get the output of any layer !!
Get index of nth largest / smallest value in list in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I want to get the name of the function / method being executed
Extract the index of the original set list that corresponds to the list of subsets.
I tried to get a list of AMI Names using Boto3
How to get a list excluding elements whose index is i ...?
10 methods to improve the accuracy of BERT
Get the column list & data list of CASTable
Get the value of the middle layer of NN
I tried to get the batting results of Hachinai using image processing
[Linux] Command to get a list of commands executed in the past
I tried to get the authentication code of Qiita API with Python.
I want to sort a list in the order of other lists
I tried to get the RSS of the top song of the iTunes store automatically
I tried to get the movie information of TMDb API with Python
I tried to display the altitude value of DTM in a graph
I measured the speed of list comprehension, for and while with python2.7.
[For beginners] I want to get the index of an element that satisfies a certain conditional expression
I want to get the path of the directory where the running file is stored.
I used gawk to find out the maximum value that goes into NF.
The story of IPv6 address that I want to keep at a minimum
I checked the list of shortcut keys of Jupyter
I tried to touch the API of ebay
I tried to correct the keystone of the image
I want to get League of Legends data ③
I want to get League of Legends data ②
I want to see a list of WebDAV files in the Requests module
Python script to get a list of input examples for the AtCoder contest
Search by the value of the instance in the list
To get the path of the currently running python.exe
I want to customize the appearance of zabbix
I want to get League of Legends data ①
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
Get the value of a specific key in a list from the dictionary type in the list with Python
I want to get started with the Linux kernel, what is the list head structure?
Convert a slice object to a list of index numbers
I want to grep the execution result of strace
I tried to summarize the basic form of GPLVM
[python] Get the list of classes defined in the module
[C language] [Linux] Get the value of environment variable
Get to know the feelings of gradient boosting trees
I want to fully understand the basics of Bokeh
Try to get the contents of Word with Golang
I measured the performance of 1 million documents with mongoDB
[Python] Get the list of ExifTags names of Pillow library
I tried to visualize the spacha information of VTuber
Python: Get a list of methods for an object