How to improve Python code speed

Xirion11

I was solving this python challenge http://coj.uci.cu/24h/problem.xhtml?abb=2634 and this is my answer

c = int(input())
l = []
for j in range(c) :
    i = raw_input().split()[1].split('/')
    l.append(int(i[1]))
for e in range(1,13) :
    print e , l.count(e)

But it was not the fastest python solution, so i tried to find how to improve the speed and i found that xrange was faster than range. But when i tried the following code it was actually slower

c = int(input())
l = []
for j in xrange(c):
    i = raw_input().split()[1].split('/')[1]
    l.append(i)
for e in xrange(1,13) :
    print e , l.count(`e`)

so i have 2 questions :

  1. How can i improve the speed of my script
  2. Where can i find information on how to improve python speed

When i was looking for this info i found sites like this one https://wiki.python.org/moin/PythonSpeed/PerformanceTips but it doesn't specify for example, if it is faster/slower to split a string multiple times in a single line or in multiple lines, for example using part of the script mentioned above :

i = raw_input().split()[1].split('/')[1]

vs

i = raw_input().split()
i = i[1].split('/')
i = i[1]

Edit : I have tried all your suggestions but my first answer is still the fastest and i don't know why. My firs answer was 151ms and @Bakuriu's answer was 197ms and my answer using collections.Counter was 188ms.

Edit 2 : Please disregard my last edit, i just found out that the method for checking your code performance in the site mentioned above does not work, if you upload the same code more times the performance is different each time, some times it's slower and sometimes faster

Bakuriu

Assuming you are using CPython, the golden rule is to push as much work as possible into built-in functions, since these are written in C and thus avoid the interpreter overhead.

This means that you should:

  • Avoid explicit loops when there is a function/method that already does what you want
  • Avoid expensive lookups in inner loops. In rare circumstances you may go as far as use local variables to store built-in functions.
  • Use the right data structures. Don't simply use lists and dicts. The standard library contains other data types, and there are many libraries out there. Consider which should be the efficient operations to solve your problem and choose the correct data structure
  • Avoid meta-programming. If you need speed you don't want a simple attribute lookup to trigger 10 method calls with complex logic behind the scenes. (However where you don't really need speed metaprogramming is really cool!)
  • Profile your code to find the bottleneck and optimize the bottleneck. Often what we think about performance of some concrete code is completely wrong.
  • Use the dis module to disassemble the bytecode. This gives you a simple way to see what the interpreter will really do. If you really want to know how the interpreter works you should try to read the source for PyEval_EvalFrameEx which contains the mainloop of the interpreter (beware: hic sunt leones!).

Regarding CPython you should read An optimization anecdote by Guido Van Rossum. It gives many insights as to how performance can change with various solutions. An other example could be this answer (disclaimer: it's mine) where the fastest solution is probably very counter intuitive for someone not used to CPython workings.

An other good thing to do is to study all most used built-in and stdlib data types, since each one has both positive and negative proporties. In this specific case calling list.count() is an heavy operation, since it has to scan the whole list every time it is performed. That's probably were a lot of the time is consumed in your solution.

One way to minimize interpreter overhead is to use collections.Counter, which also avoids scanning the data multiple times:

from collections import Counter

counts = Counter(raw_input().split('/')[-2] for _ in range(int(raw_input())))

for i in range(1, 13):
    print(i, counts[str(i)])

Note that there is no need to convert the month to an integer, so you can avoid those function calls (assuming the months are always written in the same way. No 07 and 7).

Also I don't understand why you are splitting on whitespace and then on the / when you can simply split by the / and take the one-to-last element from the list.

An other (important) optimization could be to read all stdin to avoid multiple IO calls, however this may not work in this situation since the fact that they tell you how many employees are there probably means that they are not sending an EOF.


Note that different versions of python have completely different ways of optimizing code. For example PyPy's JIT works best when you perform simply operations in loops that the JIT is able to analyze and optimize. So it's about the opposite of what you would do in CPython.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Improve python code in terms of speed

From Dev

Selenium code in PyCharm(Python) running slow, how to improve speed?

From Dev

Selenium code in PyCharm(Python) running slow, how to improve speed?

From Dev

How to improve python import speed?

From Dev

MatLab code speed and optimization. How to improve?

From Dev

MatLab code speed and optimization. How to improve?

From Dev

How to improve the speed of my selection process, python

From Dev

how to improve speed in database?

From Dev

How to improve the performance of this Python code?

From Dev

How to improve the performance of this Python code?

From Dev

Is it possible to improve this code to gain speed?

From Dev

How can i improve my multithreading speed and effciency in python?

From Dev

how to improve speed with using RallyAPIForJava

From Dev

How to improve Golang compilation speed?

From Dev

How to improve persistence USB speed?

From Dev

How to improve fragment loading speed?

From Dev

How to improve speed of send mail?

From Dev

Android how to improve the download speed

From Dev

How to improve persistence USB speed?

From Dev

how to improve speed with using RallyAPIForJava

From Dev

How to improve speed of parsing in Jsoup

From Dev

How to improve page speed in google?

From Dev

How can I improve this code in python?

From Dev

How to improve time of python code with database queries

From Dev

How can I speed this python code up?

From Dev

Improve Python code performance

From Dev

Improve Speed of Python Script: Multithreading or Multiple Instances?

From Dev

Python: Improve the speed of Euclidean distance calculation in a class

From Dev

Improve Speed of Python Script: Multithreading or Multiple Instances?

Related Related

HotTag

Archive