Python | efficiency and performance
 

Lets say I'm going to save 100 floating point numbers in a list by running a single script, most probably it will take some memory to process.So if this code executes every time as a requirement of an application there will be performance hits, so my question is how to maintain efficiency in order to gain performance.

Mock-up code:

def generate_lglt():
    float1, float2 = 27.2423423, 12.2323245
    lonlats = []
    for val in range(100, 0, -1):
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
        lonlats.append(random.uniform(float1, float2))
    print lonlats

Thanks.

http://www.stackoverflow.com/questions/2457363/

        

There are 2 answer(s) to this question.


If generate_lglt() is going to be called a lot of different times, you may want to keep from regenerating the same range(100,0,-1) with every call of the code. You may want to cache that generated range somewhere and use it over and over again.

Also, if you are going to be exiting a for loop without completing each iteration, use xrange instead of range.


Bottlenecks occur at unexpected places, so never optimize code just because you think it might be the right code to try to improve. What you need to do is

  1. Write your program so that it runs completely.
  2. Develop tests to make sure your program is correct.
  3. Decide whether your program is too slow.
    • There is a good chance you will quit at this step.
  4. Develop performance tests that run your program realistically.
  5. Profile the code in its realistic performance tests using the cProfile module.
  6. Figure out what algorithmic improvements can improve your code's performance.
    • This is usually the way to improve speed the most.
  7. If you are using the best algorithm for the job, perform micro-optimizations.
    • Rewriting critical parts in C (possibly using Cython) is often more effective than in-Python micro-optimizations.

Related Questions

Related python Video tutorials from Youtube.


Google Python Class Day 1 Part
Google Python Class Day 1 Part 1: Introduction and Strings. By Nick Parlante. Support materials and
Google Python Class Day 1 Part 1
Google Python Class Day 1 Part 1: Introduction and Strings. By Nick Parlante. Support materials and exercises: code.google.com

Python Programming Tutorial -
Part 11 - www.youtube.com Slice python.
Python Programming Tutorial - 10 - Slicing
Part 11 - www.youtube.com Slice python.

Google Python Class Day 1 Part
Google Python Class Day 1 Part 2: Lists, Sorting, and Tuples. By Nick Parlante. Support materials an
Google Python Class Day 1 Part 2
Google Python Class Day 1 Part 2: Lists, Sorting, and Tuples. By Nick Parlante. Support materials and exercises: code.google.com

Google Python Class Day 1 Part
Google Python Class Day 1 Part 3: Dicts and Files. By Nick Parlante. Support materials and exercises
Google Python Class Day 1 Part 3
Google Python Class Day 1 Part 3: Dicts and Files. By Nick Parlante. Support materials and exercises: code.google.com

Kolmogorov complexity with Pyt
The basics of Kolmogorov complexity, explained using the Python programming language. Download Pytho
Kolmogorov complexity with Python
The basics of Kolmogorov complexity, explained using the Python programming language. Download Python at www.activestate.com More information on Kolmogorov complexity: www.scholarpedia.org en.wikipedia.org www.scholarpedia.org talkorigins.org If you want to learn more about Python, check out docs.python.org Inspired by randyhelzerman's series "Kolmogorov Complexity using Prolog" ( tinyurl.com ). The music is preview.tinyurl.com twitter.com www.facebook.com myspace.com Honors for this video (9) #35 - Most Discussed (Today) - Education #68 - Most Viewed (Today) - Education - Australia #49 - Most Viewed (Today) - Education - Canada #87 - Most Viewed (Today) - Education - Israel #69 - Most Viewed (Today) - Education #76 - Most Viewed (Today) - Education - Netherlands #53 - Most Viewed (Today) - Education - Sweden #61 - Top Favorited (Today) - Education #32 - Top Rated (Today) - Education

Python tutorial for beginners
Thank you Below is the C2F conversion calculator, keep in mind it is only 4 lines long. def C2F(): C
Python tutorial for beginners
Thank you Below is the C2F conversion calculator, keep in mind it is only 4 lines long. def C2F(): Celsius = input('Enter degrees in Celsius: '); Fahrenheit = (9.0 / 5.0) * Celsius +32; print Celsius, 'Celsius =', Fahrenheit, 'Fahrenheit';

Tetris Artificial Intelligence
This is my second attempt in producing AI for Tetris. Grid size is 10x20 I made this in Python It al
Tetris Artificial Intelligence (AI) #2
This is my second attempt in producing AI for Tetris. Grid size is 10x20 I made this in Python It almost always does more than 1000 lines, and most I've seen it do was about 5000. Unfortunately I don't possess enough computing resources to have it learn more and fine tune the parameters as much as I'd like, but this will do for now! :) music: Tetris by 2am

Google Python Class Day 2 Part
Google Python Class Day 2 Part 1: Regular Expressions. By Nick Parlante. Support materials and exerc
Google Python Class Day 2 Part 1
Google Python Class Day 2 Part 1: Regular Expressions. By Nick Parlante. Support materials and exercises: code.google.com

Sketcher: Program draws pictur
This is a computer program I made that attempts to imitate the way an artist would draw an image wit
Sketcher: Program draws pictures
This is a computer program I made that attempts to imitate the way an artist would draw an image with a pencil. description for the mathematically inclined: The program simply follows the perpendicular of the first order approximation of the image gradient of the pixel intensities. Every path containing a high number of high intensity gradients is stored as a stroke. These strokes are ranked in order of "goodness", that is a function of both the length and intensity of that stroke. The pencil then shades these strokes in around the image, starting with the strong strokes, and going towards the light strokes at the end. Strokes that are close to each other are likely to be drawn consecutively for more pleasing visual effect. made using python (with pygame library)

Google Python Class Day 2 Part
Google Python Class Day 1 Part 1: Closing Thoughts. By Nick Parlante. Support materials and exercise
Google Python Class Day 2 Part 4
Google Python Class Day 1 Part 1: Closing Thoughts. By Nick Parlante. Support materials and exercises: code.google.com

Game Development Tutorial - 18
How to add vectors using python and pygame.
Game Development Tutorial - 18 - Adding Vectors
How to add vectors using python and pygame.

Ira Krakow's Blender 2.50 Pyth
Discuss this video at: forum.irakrakow.com The purpose of this video is to give you an idea of the P
Ira Krakow's Blender 2.50 Python Part 1 (Environment)
Discuss this video at: forum.irakrakow.com The purpose of this video is to give you an idea of the Python scripting environment in the new Blender 2.5, as well as to give you places to start if you either don't know Python at all, or are new to programming. Don't be afraid if you are not a programmer. Python is not a difficult language to learn. This tutorial is based on Blender Labrat (mfoxdogg) tour of the Python setup in Blender 2.5, and the Blender 2.5 Python API introduction at blender.org. I based this tutorial on Blender 2.5 Alpha 1, released in mid-February, 2010. The basic document is Blender 2.6 Python API (the Application Programming Interface for Blender) document: www.blender.org The Wiki page to start learning te Blender 2.5 Python API is at: wiki.blender.org To learn Python itself, start at www.python.org and click the Tutorials tab. You can do your learning entirely inside Blender, using the new Python 3.1 console that's a part of Blender 2.5. This API is undergoing modification. You're looking at the version as of February 15, 2010. The basic module is bpy, which is a totally different API than Blender 2.4x. The functions are grouped into different types. For example, bpy.ops documents the Blender Python operators. Under Mesh for example, you can see the paramters for anything you can do with a mesh, in alphabetical order.

Game Development Tutorial - 17
What unit vectors are and how to find them with python and pygame.
Game Development Tutorial - 17 - Unit Vector
What unit vectors are and how to find them with python and pygame.

Python 3 Tutorial: 5 - Functio
In this Python 3 tutorial, I cover the basics of functions in Python as I briefly explain what funct
Python 3 Tutorial: 5 - Functions Intro.
In this Python 3 tutorial, I cover the basics of functions in Python as I briefly explain what functions are, and show how to create and call them. I also show what parameters are, and how to create and call functions that contain parameters.

Blender GLSL - Parallax Mappin
A test of a concept that came to mind. To use material nodes to create a parallax mapping effect. Th
Blender GLSL - Parallax Mapping Effect w/ Material Nodes
A test of a concept that came to mind. To use material nodes to create a parallax mapping effect. The material uses a bunch of nodes to basically combine normal and UV inputs from a geometry input, and then use different levels of displacement by using a bump map and RGB curves, then, a shaded material is multiplied over the shadeless displaced textures. The object in the video is one plane, and no python was used.

Free 3d Animation Software + G
Here is the Download Page for the 3d animation program blender! www.blender.org Here is the download
Free 3d Animation Software + Game Engine
Here is the Download Page for the 3d animation program blender! www.blender.org Here is the download for python 2.6.2 (blender 2.49 requires it to run best) www.python.org The newest version of python (for blender 2.5+) www.python.org Check out some of my blender tutorials on youtube or my website. Youtube Blender Game Engine Tutorials www.youtube.com www.aceroc.com ------------------Things you Can make with blender--------------------- Amazing blender movie www.youtube.com Blender Game Engine Demo www.youtube.com Blender Game Engine Game www.youtube.com Blender 3d Animation Fluid Animation www.youtube.com ----------------------------------------------------------------------------------------- -Some Blender Tutorials- www.blender.org (Tutorials) www.tutorialsforblender3d.com (Tutorials) -----------------------------------------------------------------------------------------

Ira Krakow's Python 3 Tutorial
Discuss this video, and read the script for it, at: forum.irakrakow.com The goal of this tutorial is
Ira Krakow's Python 3 Tutorial Part 1 (Expressions)
Discuss this video, and read the script for it, at: forum.irakrakow.com The goal of this tutorial is to introduce Blender users, whether or not they are programmers, to Python 3. Blender 2.5 supports Python 3 scripts, which can let you program Blender behavioer very precisely. Of course, before you can create Blender scripts, you need to know Python 3. We will use the Blender 2.5 Scripting environment, which includes a Python console that supports most (but not all) of Python 3, as well as a fully featured text editor, with advanced features such as autocomplete and automatic indenting of code. I will point out where Blender's Python implementation differs from the standard Python 3 distribution. There are minor differences, none that should prevent you from writing fully featured scripts in Blender 2.5. Before working through the examples in this tutorial, you need to download Blender 2.5. You can do this at: www.blender.org This series is based on the Python 3 tutorial, at: docs.python.org It is not meant to cover all aspects of Python 3. I will cover enough so that you should be able to write Python scripts in Blender 2.5. This tutorial, the first in my Python 3 Programming series, will cover the Blender 2.5 Python console, how to create simple expressions and assignments, and how to use the Python 3 console as a calculator. We'll go into more depth in further tutorials.

Python 3 Tutorial: 6 - More Fu
In this tutorial I show how to create and call functions that contain default argument values. I als
Python 3 Tutorial: 6 - More Functions
In this tutorial I show how to create and call functions that contain default argument values. I also explain what global and local variables are in Python.

Python 2: Getting around Pytho
Beginners' tutorial for the Python programming language. This video focuses on importing modules and
Python 2: Getting around Python modules
Beginners' tutorial for the Python programming language. This video focuses on importing modules and using the "inspect" module to explore the source code of modules you are using.

Blog Visitor Statistics via PH
For this project, my goal was to develop an LED display device that could provide me with real-time
Blog Visitor Statistics via PHP, Python, and an Arduino + Multicolor LED
For this project, my goal was to develop an LED display device that could provide me with real-time statistics about my website (www.jeremyblum.com). I designed my project to collect and display three types of information 1.Number of Visits by Human Users in last 24 hours, week, and month 2.Browsers Being used by Visiting Users 3.Operating Systems of Visiting Users A multicolor LED is used to convey this information. View my other mini projects for this class at www.youtube.com Download the Source Code and Schematics for this Project here: jeremyblum.com

Game Programming Tutorial 1 in
This is first tutorial in my series of videos on Game Programming in Python. This video I will intro
Game Programming Tutorial 1 in Python: Introduction
This is first tutorial in my series of videos on Game Programming in Python. This video I will introduce you to the tools you will need, provide you with some starter code, explain how it works, and then I will give you a seizure :) For help/ FAQ/ code/ links visit: karpathy.ca

GLRC - Monty Python's Flying C
Originally Uploaded: Sun 14 Sep 2008 04:27:41 PDT AKA Monty Python's Flying Circus: The Computer Gam
GLRC - Monty Python's Flying Circus (Multiple Formats)
Originally Uploaded: Sun 14 Sep 2008 04:27:41 PDT AKA Monty Python's Flying Circus: The Computer Game He's Not a guru, he's a very naughty boy! In this episodes retro corner, Larry looks at a game that surprisingly went under the radar of most gamers, infact there's not even a wikipedia page about it (which is annoying as we'd have just nicked a load of facts off that) even more suprising when you realise it was made by the same team that made Tomb Raider! But we dedicate this episode to one of Britain's most famous comedy TV show, so sit down and get your albatross and crunchy frog ready to snack on as we sit down to play a very bizarre video game!

Google Python Class Day 2 Part
Google Python Class Day 2 Part 2: Utilities: OS and Commands. By Nick Parlante. Support materials an
Google Python Class Day 2 Part 2
Google Python Class Day 2 Part 2: Utilities: OS and Commands. By Nick Parlante. Support materials and exercises: code.google.com

GLRC - Monty Python's Flying C
AKA Monty Python's Flying Circus: The Computer Game He's Not a guru, he's a very naughty boy! In thi
GLRC - Monty Python's Flying Circus (Multiple Formats)
AKA Monty Python's Flying Circus: The Computer Game He's Not a guru, he's a very naughty boy! In this episodes retro corner, Larry looks at a game that surprisingly went under the radar of most gamers, infact there's not even a wikipedia page about it (which is annoying as we'd have just nicked a load of facts off that) even more suprising when you realise it was made by the same team that made Tomb Raider! But we dedicate this episode to one of Britain's most famous comedy TV show, so sit down and get your albatross and crunchy frog ready to snack on as we sit down to play a very bizarre video game!

Dell Free Software commercial
Dell's new commercial, with a Monty Python touch. This new Dell advertisement features Sir Michael D
Dell Free Software commercial advertisement
Dell's new commercial, with a Monty Python touch. This new Dell advertisement features Sir Michael Dell, a knight whose quest is to free the human kind from the dangers of proprietary software and hardware. Many other 'heroes' will join him in his fight. Dell, dell, dell, dell...

American McGee has worked on t
American McGee is a legend of interactive entertainment. He wrote and produced "Bad Day LA", the new
American McGee has worked on the world's greatest games - he
American McGee is a legend of interactive entertainment. He wrote and produced "Bad Day LA", the new XBOX video game that is a memorably macabre romp through the eccentricities of Los Angeles, with its violence and irony reflecting influences from South Park to Monty Python. He also created the dark new "Alice in Wonderland" film with Sarah Michell Gellar as Alice (sneak previews included here). In this interview, American describes his life experience with games. He started at id Software across design, programming, music management, sound design and art crafting the 1st/3rd person shooter genre as it is known today. He contributed to titles including: "Doom," "Doom II," "Final Doom," "Ultimate Doom," "Quake", "Quake II,", and "Quake II Deathmatch Pack." In this interview, he describes what it was like to be involved in such well-known titles and what he has learned from his mentorship at the X|Media Lab in Singapore. American's own company, the Mauretania Import and Export Company, acts as an IP creation house today, adapting his original stories into films, books and merchandise. American speaks frankly in this interview about everything from why his own name is so often included in his game titles to where he thinks American culture is headed in the 21st century - and his new work, a prequel to the Wizard of Oz with Jerry Bruckheimer.

Python 3 Tutorial: 10 - If sta
The famous if statement, along with the elif and else statements
Python 3 Tutorial: 10 - If statement
The famous if statement, along with the elif and else statements

Python 3 Tutorial: 9 - Input
This tutorial covers the basics of the input function, and shows how to use the int function as well
Python 3 Tutorial: 9 - Input
This tutorial covers the basics of the input function, and shows how to use the int function as well.

Ira Krakow's Python Scripting
Discuss this video at forum.irakrakow.com and network with other Blender 3D users. Read the text of
Ira Krakow's Python Scripting Setup Blender 2.49b Tutorial
Discuss this video at forum.irakrakow.com and network with other Blender 3D users. Read the text of this tutorial at blender3dvideos.blogspot.com In addition to everything else, Blender is a programming environment, with Python its language of choice. Even if you don't plan on creating Python scripts in Blender, you should understand the Python environment. Python is how Blender talks to you. The purpose of this tutorial is to introduce you to Blender's Python environment. Understanding this environment should help you understand how Blender works as well. Of course, if you want to write Python scripts to make Blender behave exactly how you want, becoming familiar with the Blender Python environment is the first step. Even if you don't program, just understanding how to run a script will allow you to run Python programs, called scripts, written by others. For starters, go to the Blender Python Scripts catalog, at wiki.blender.org You can find the links to Web sites mentioned here at blender3dvideos.blogspot.com, the companion blog to my tutorials.

Ira Krakow's Python 3 Tutorial
In this tutorial, we are going to see how to write and call functions in Python 3, as well as how to
Ira Krakow's Python 3 Tutorial Part 6 (Functions)
In this tutorial, we are going to see how to write and call functions in Python 3, as well as how to pass arguments, both as keyword and value, to a Python function. I will also introduce you to some of the neat features of the Blender 2.5 text editor, such as source code color coding, which help make coding Python scripts much easier. This video is based on the Python 3.1 function tutorial at: docs.python.org

Fedora13: More Powerful Python
press.redhat.com Dave Malcolm, an engineer for the Fedora Project, explains the new version of Pytho
Fedora13: More Powerful Python
press.redhat.com Dave Malcolm, an engineer for the Fedora Project, explains the new version of Python available in Fedora 13 as well as tools for porting and debugging code written in Python and C++. Dave also gives demos of pyfuntop, a SystemTap script for viewing Python function calls, and also a crash report for Python with and without the debugging hooks.

HD - Think Like A PYTHON - De
Dual Commentary me and My friend PYTHON.... Comment and Plz RATE RATE RATE
HD - Think Like A PYTHON - De UnBoRn
Dual Commentary me and My friend PYTHON.... Comment and Plz RATE RATE RATE

[HQ] Longest Common Substring
Explanation of how the LCS algorithm works (a Python implementation). Algorithm adapted from Wikiboo
[HQ] Longest Common Substring (LCS) algorithm walk through
Explanation of how the LCS algorithm works (a Python implementation). Algorithm adapted from Wikibooks.org here: bit.ly Full blog here: - Jay Liew, Security Researcher @ Websense Security Labs

Tob's Blender Python tutorial
This makes a good start at explaining Classes and Objects in the Python programming language. It exp
Tob's Blender Python tutorial 105 Classes and Objects 1/2
This makes a good start at explaining Classes and Objects in the Python programming language. It explains how to use them and how to make them and how they relate to each other. The next tutorial provides another (hopefully useful) chunk of information about Python's Classes and Objects.

Python 3 Tutorial: 8 - Creatin
In this tutorial, I finally show you how to create, save, and execute a program using Python's built
Python 3 Tutorial: 8 - Creating Programs
In this tutorial, I finally show you how to create, save, and execute a program using Python's built in editor.

Make your own Nintendo DS Game
Cheap mod chips plus free dev tools equals hello world on the Nintendo DS! Jason Appelbaum joins us
Make your own Nintendo DS Games for free! Tunneling on ...
Cheap mod chips plus free dev tools equals hello world on the Nintendo DS! Jason Appelbaum joins us to geek out about code up the dual screen'd programmers playground that is NDS homebrew. Matt has answers to your questions about Virtual Routers, Paul comes out from behind the camera to school us on SSH Tunneling for the Mac, and Darren's featuring some pretty Python & PHP to tunnel cross platform.

Python Programming Lesson One
In this lesson we will learn how to make comments and how to use print(). Next time we will learn ho
Python Programming Lesson One
In this lesson we will learn how to make comments and how to use print(). Next time we will learn how to use calculator functions! Comment, rate, and subscribe, please!

Tutorial: Using Python & gtk.B
Part 1 of 5 (Roughly 40minutes total time.) I would reccomend watching in HD, as big as possible if
Tutorial: Using Python & gtk.Builder/Glade to create a simple GUI application, Part 1.
Part 1 of 5 (Roughly 40minutes total time.) I would reccomend watching in HD, as big as possible if you are planning to follow the tutorial, otherwise some aspects may be hard to see. Just as a quick note, some of the observant ones out there might notice I say at the beinning I plan on making the result text area uneditable by the user. I did this in the original tutorial, but forgot about it in this one. To do this, while in glade, choose the result entry (named entryResult in the tutorial) and click on 'Editable' to make it 'No'. Also please excuse the occasional background noise and the like, I was only using a cheap microphone. It was all recorded first-try, unscripted, with only minor editing too, so please excuse all of the "erm"s. Source Code: www.lattyware.co.uk Original Tutorial: www.overclock.net

Running quake with a kinect an
Twitter @diarmuid I used the kinect attached to a linux laptop with a python based web server servin
Running quake with a kinect and a hurley
Twitter @diarmuid I used the kinect attached to a linux laptop with a python based web server serving up point data to an imac. The imac runs a java program that calls the linux web server and generates key and mouse events. Quake live is set up with the appropriate keystrokes. When the java program starts it gives me 15 seconds to get onto Quake. As I move my hurley around infront of the kinect, you can see the quake environment moving appropriately. The video resolution is poor because of some upload issue.

Python 3 Tutorial: 7 - Modules
This tutorial is all about...you guessed it! Modules.
Python 3 Tutorial: 7 - Modules
This tutorial is all about...you guessed it! Modules.

First 5 Minutes Programming wi
How do you get started with Python Programming in just 5 Minutes? Here I show you, using Python 2.5
First 5 Minutes Programming with Python
How do you get started with Python Programming in just 5 Minutes? Here I show you, using Python 2.5 and the bundled IDLE editor, how to start writing your first program and where to go next for help and tutorials. First we look at python.org's documentation and HOWTO section, then we move to my Python Resources for Newbies video at ShowMeDo to learn about on-line tutorials, newsgroups, news, download and community sites. This video is a part of our Python Collection. Finally we look at Guido van Rossum's Python Tutorial and use the bundled IDLE editor to write and run a one-line Python program. Uploaded by the author. Author is a co-founder of ShowMeDo.com (we host Python tutorial videos).

Python Lesson 1: Hello World a
My first video tutorial for programming in Python, more to come soon (I hope!)
Python Lesson 1: Hello World and Basic Variables
My first video tutorial for programming in Python, more to come soon (I hope!)

Game Programming Tutorial 6 in
We implement collision detection on our agents from tutorial 5, so they never overlap. Total of 10 l
Game Programming Tutorial 6 in Python: Collision detection and resolution
We implement collision detection on our agents from tutorial 5, so they never overlap. Total of 10 lines of code :) In process we will get more familiar with vectors and what we can use them for. Code and few comments: karpathy.ca

Python Tutorial for beginners
This is a tutorial for people starting out in the world of programming. This series of tutorials cov
Python Tutorial for beginners - 001 - Comments and Strings [HD]
This is a tutorial for people starting out in the world of programming. This series of tutorials covers the very basics of python. I hope you learn a lot from me and have fun in the process. This tutorial in particular covers print and comment functions.thenewboston python for beginners tutorial thenewboston python for beginners tutorial thenewboston python for beginners tutorial thenewboston python for beginners tutorial thenewboston python for beginners tutorial

Post you comment here

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

There are 0 comment(s) to this page.



The questions and answers taken from stackoverflow.com's public data dump which is licensed under the cc-wiki license.
Logo, website design and layout ©2011 CodingTiger.com