Make a program faster (python)

A forum for general discussion of the Python programming language.

Make a program faster (python)

Postby Geki862 » Fri Sep 16, 2016 1:35 pm

Hello, I have a program in python and he must work in 20secondes. But I don't understand how I can make it faster.

there is th program bellow :

Code: Select all
(imax,maximum)=(0,0)
for entier in range(1,3000001):
    i=1
    a
=entier
    while entier
>1:
        if entier%2==0:
            entier = entier/2
            i
+=1
        else 
:
            entier = (entier*3+1)/2
            i
+=2
    if i
>imax:
        imax=i
        maximum 
= a
print(maximum)
Last edited by micseydel on Fri Sep 16, 2016 2:37 pm, edited 1 time in total.
Reason: Code tags. Initial post lock.
Geki862
 
Posts: 2
Joined: Fri Sep 16, 2016 1:33 pm

Re: Make a program faster (python)

Postby micseydel » Fri Sep 16, 2016 2:44 pm

Where is this from? Can you describe the goal in natural language? Is this a named algorithm? What are entier and imax? What have you tried to speed it up? Have you done any complexity analysis, profiling, or anything else?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Make a program faster (python)

Postby micseydel » Fri Sep 16, 2016 2:51 pm

I've identified this as the Collatz sequence. You should look into memoization and/or dynamic programming as techniques to speed this up.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Make a program faster (python)

Postby Geki862 » Fri Sep 16, 2016 2:57 pm

thank you i will check
Geki862
 
Posts: 2
Joined: Fri Sep 16, 2016 1:33 pm

Re: Make a program faster (python)

Postby snippsat » Sun Sep 18, 2016 1:43 pm

The lazy way PyPy
So a really big difference ;)
Code: Select all
#PyPy version 5.3.1
λ pypy max_speed.py
2298025
Code used 3.21 seconds

#Python 3.4
λ python max_speed.py
2298025
Code used 5.83 minutes
We will be moving to python-forum.io on October 1 2016
User avatar
snippsat
 
Posts: 1251
Joined: Thu Feb 21, 2013 12:04 am


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 5 guests

cron