Python - fastest code

Hi team

Working on CODE CHALLENGE: DICTIONARIES # Largest Value , I googled some keywords and wrote two codes. Both work, but for sake of developing something, how can I calculate how fast is each or how much memory will it take(without counting the lines as it could be impractical for longer codes)

Kindly help. Thank you

def max_key1(my_dictionary):
  output = [(value, key) for key, value in my_dictionary.items()]
  print(output)
  return (max(output)[1])
def max_key(my_dictionary):
  largest_key = " "
  largest_value = float(-9999)
  for k , v in my_dictionary.items():
    if largest_value < v :
      largest_value = v
      largest_key = k
  return largest_key

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hi @rdb20

If you’re just looking to time small sections of code, for example comparing your two functions here, you can use the Python timeit module.

timeit essentially allows you to run your code several times, and see how long it takes.

Which means, we can do this:

import timeit

def max_key1(my_dictionary):
  output = [(value, key) for key, value in my_dictionary.items()]
  #print(output)
  return (max(output)[1])

def max_key(my_dictionary):
  largest_key = " "
  largest_value = float(-9999)
  for k , v in my_dictionary.items():
    if largest_value < v :
      largest_value = v
      largest_key = k
  return largest_key

test_dict = {
  20000 : 190243259,
  894 : 498,
  1234567890 : 999,
}

t1 = timeit.timeit("max_key(test_dict)",globals=globals())
t2 = timeit.timeit("max_key1(test_dict)",globals=globals())

print("'max_key' took %05f seconds to run 1 million times..." % t1)
print("'max_key1' took %05f seconds to run 1 million times..." % t2)

I’ve only created a very small test dictionary here to demonstrate the function.

If I run this in a repl, I get the following:

Python 3.6.8 (default, Jun 11 2019, 01:21:42)
[GCC 6.3.0 20170516] on linux
'max_key' took 1.332122 seconds to run 1 million times...
'max_key1' took 2.192491 seconds to run 1 million times...

Is that any help? :slight_smile:

Thank you pitycoder. That was very helpful. WBut what is global = global in timeit syntax and what other attributed can it take?

The parameter globals tells timeit where to look for the functions that it is being asked to run. (By default, it looks within the timeit module.) The value globals() assigned to it specifies the “global namespace”, which is essentially everything out-dented all the way to the left-hand margin in the script you are looking at. Type print(globals()) to see what is there.

Before globals=globals() was added (fairly recently), you needed to use a mildly complex setup process to use timeit.

For some possible other values of globals, see the checked answer here.

:arrow_right: Edited to correct the following error: the relevant parameter of the timeit.timeit() method is globals, not global, which is a Python statement. As originally stated, the value is globals(), a builtin function.

1 Like

Something else to consider about global… When a variable exists in the global namespace, we can still access it from within a function. That is not a problem. However, if we wish to have the function mutate the global, we need to vector it to the global namespace.

>>> var = 'value'
>>> def foo(x):
	global var
	var = x

	
>>> foo('new value')
>>> var
'new value'
>>> def bar(x):
	var = x


>>> bar('value')
>>> var
'new value'
>>> 

Notice that bar did not mutate the global.

1 Like