[Tutor] Timing a python program

2015-01-16 Thread CL Talk
Hello - I have a python program that reads data from data files and
does manipulation on the data and writes back to output files. I am
developing this program on a macbook with python 2.7.6.

This program takes close to 10 minutes on my machine to run. But, the
issue is that it is not take the same amount of time on almost similar
data files.

I am trying to see how I can time the whole program as well as various
functions that are part of the program.

Say, that I have 5 functions in this program how can i time the whole
program and time each individual function for every run?

Thanks in advance for your help and ideas.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timing a python program

2015-01-16 Thread Ben Finney
CL Talk  writes:

> I am trying to see how I can time the whole program as well as various
> functions that are part of the program.

The term for this dynamic timing introspection of parts of the system is
“program profiling”; you are seeking a “profiler”
https://en.wikipedia.org/wiki/Profiling_%28computer_programming%29>.

Python has profilers installed by default in the standard library
https://docs.python.org/3/library/profile.html>. See that
documentation for how to profile your program.

-- 
 \  “It is … incumbent upon us to recognize that it is |
  `\inappropriate for religion to play any role in issues of state |
_o__)[of] a modern democracy.” —Lawrence M. Krauss, 2012-05-28 |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timing a python program

2015-01-16 Thread Alan Gauld

On 16/01/15 05:02, CL Talk wrote:


I am trying to see how I can time the whole program as well as various
functions that are part of the program.



That's called profiling and there is a Python profiler.

Here's the official doc page

https://docs.python.org/2/library/profile.html

And here's the 'module of the week' description of using it:

http://pymotw.com/2/profile/


You might also find the timeit module handy.

hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timing a python program

2015-01-16 Thread Mark Lawrence

On 16/01/2015 08:52, Alan Gauld wrote:

On 16/01/15 05:02, CL Talk wrote:


I am trying to see how I can time the whole program as well as various
functions that are part of the program.



That's called profiling and there is a Python profiler.

Here's the official doc page

https://docs.python.org/2/library/profile.html

And here's the 'module of the week' description of using it:

http://pymotw.com/2/profile/


You might also find the timeit module handy.

hth


For non Luddites https://docs.python.org/3/library/profile.html :)

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Improving My Simple Game Code for Speed, Memory and Learning

2015-01-16 Thread WolfRage

On 01/12/2015 04:47 PM, Mark Lawrence wrote:


I haven't looked carefully at your code but there's always a smell in
Python when you see structure[x][y].  Can you change the grid so you
always write something like:-

for row in grid:
 for cell in row:
 process(cell)

I say this as I'm all for short term pain, long term gain, especially
when it's guaranteed to eliminate "list index out of range" errors.

Revisiting this, I think I will write a function specifically to perform 
look-ups, kind of like your process() function. My purpose is going to 
be to prevent out of bounds look-ups. Although none of my posted code 
has had potential for this so far, I am writing more code to help play 
the game and one of the things I am needing now is to look at the 
neighbours of a selected node. This look-up can provide me two errors, 
one is the index out of range the and the more subtle error is going 
backwards in the list and giving me the end of the list which is not a 
valid neighbour.

I will also use this look-up throughout the rest of my code where possible.
Thank you Mark.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timing a python program

2015-01-16 Thread Alan Gauld

On 16/01/15 09:20, Mark Lawrence wrote:


https://docs.python.org/2/library/profile.html


For non Luddites https://docs.python.org/3/library/profile.html :)



Yeah, but the OP said it was for 2.7.6 on a Mac...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 131, Issue 38

2015-01-16 Thread bw_dw

> Hello,
> I'm new the the group and new to programming in Python.
> Snipped..
> Sincere thanks
> d

> From: Emile van Sebille 
> Check out http://it-ebooks.info/book/172/
> Emile



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 131, Issue 38

2015-01-16 Thread bw_dw
THANKS EMILE!!!
I downloaded the e-book and am having fun with it!!
Much appreciate :-]



> > Hello,
> > I'm new the the group and new to programming in Python.
> > Snipped..
> > Sincere thanks
> > d
> 
> > From: Emile van Sebille 
> > Check out http://it-ebooks.info/book/172/
> > Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timing a python program

2015-01-16 Thread Albert-Jan Roskam


On Fri, Jan 16, 2015 7:34 PM CET Alan Gauld wrote:

>On 16/01/15 09:20, Mark Lawrence wrote:
>
>> https://docs.python.org/2/library/profile.html
>>
>> For non Luddites https://docs.python.org/3/library/profile.html :)
>
>
>Yeah, but the OP said it was for 2.7.6 on a Mac

python -m cProfile myscript.py
python -m timeit ''
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timing a python program

2015-01-16 Thread Danny Yoo
On Thu, Jan 15, 2015 at 9:02 PM, CL Talk  wrote:
> Hello - I have a python program that reads data from data files and
> does manipulation on the data and writes back to output files. I am
> developing this program on a macbook with python 2.7.6.
>
> This program takes close to 10 minutes on my machine to run. But, the
> issue is that it is not take the same amount of time on almost similar
> data files.
>
> I am trying to see how I can time the whole program as well as various
> functions that are part of the program.
>
> Say, that I have 5 functions in this program how can i time the whole
> program and time each individual function for every run?


The use of the profiler, as the others have discussed, is the right
approach here.  It's hard to say what's really going on without good
timing data, and the profiler should help.

Once you get that data, please feel free to share it on the mailing
list if you can; folks here can help analyze what you're seeing too.
Also, please put your source code somewhere for folks to inspect.

Finally, if you can say how large your input and output are, that
would also be helpful.  If we're talking about megabytes, then since
you're seeing minutes of runtime, I'd expect that the algorithms used
are prime culprits, and good targets for chasing for optimizations.

But if we're talking about terabytes, maybe your program is fine, and
you just have a lot of data that needs chugging through.  :P


Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] menu based programs

2015-01-16 Thread Siya 360
Hi,

Am still learning Python, and i have been tasked with writing a used menu, 
where once session is initialised by user inputing a used code e.g. *123#, it 
displays menu to user with option

i want to learn how to write code to interact with the user, in a sequence 
where if option 1, then display a, if 2 then display b, if 3 display c, if 4 
display exit, if 1 selected, leads to option a, which in turn has it own options

please assist?

kind regards,
Siya
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] menu based programs

2015-01-16 Thread Alan Gauld

On 16/01/15 23:39, Siya 360 wrote:

i want to learn how to write code to interact with the user,

> in a sequence where
> if option 1, then display a,
> if 2 then display b,
> if 3 display c,
> if 4 display exit,


if 1 selected, leads to option a, which in turn has it own options



You don't say how these menus are presented.
One option is the cmd module which has a lot of built in features such 
as context sensitive help screens. But it is similar in style ton the 
Python help() command which may not be what you want.


Simple text based menus are just built using print statements
(triple quoted multi-line strings often help a lot)

Here is a very simple example modelled on your description

#
def readMenu(menu):
while not (1<= choice <= len(menu) ):
   for index, item in enumerate(menu,start=1):
   print (index, item)
   choice = input("\nchoose an item: ")
return choice-1

def display(choice):
print( 'ABC'[choice] )

menu = ['display A','display B', display C', 'exit']

while True:
   choice = readMenu(menu)
   if choice == 3:
  break
   else: display(choice)



You can use a table driven approach with a data structure to link menu 
choices to screens. But without knowing what you want the application to 
look like it's hard to give solid advise


And if you want a GUI then it's a whole new ball game...

hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor