Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:57:22+00:00 2026-05-27T15:57:22+00:00

EDIT: I ran the python profiler and the two most time-consuming things (this is

  • 0

EDIT: I ran the python profiler and the two most time-consuming things (this is after I decided to comment out the webbrowser portion and Firefox portion of the code, because I knew they were going to be the slowest part…) , the slowest part of my program is re.findall and re.compile and also (len) and (append to list).

I don’t know if I should post all of my code on here at once because I worked really hard on my program (even if it isn’t too good), so for now I’m just going to ask…How do I make my Python program faster?

I have 3 suspects right now for it being so slow:

  1. Maybe my computer is just slow

  2. Maybe my internet is too slow (sometimes my program has to download the html of web pages and then it searches through the html for a specific piece of text)

  3. My code is slow (too many loops maybe? something else? I’m new to this so I wouldn’t know!)

If anyone could offer me advice, I would greatly appreciate it!

Thanks!

EDIT:

My code uses lots of loops I think…also, another thing is that for the program to work you have to be logged in to this website: http://www.locationary.com/

from urllib import urlopen
from gzip import GzipFile
from cStringIO import StringIO
import re
import urllib
import urllib2
import webbrowser
import time
from difflib import SequenceMatcher
import os

def download(url):
    s = urlopen(url).read()
    if s[:2] == '\x1f\x8b': # assume it's gzipped data
        with GzipFile(mode='rb', fileobj=StringIO(s)) as ifh:
            s = ifh.read()
    return s

for t in range(3,39):
    print t
    s = download('http://www.locationary.com/place/en/US/Utah/Provo-page' + str(t) + '/?ACTION_TOKEN=NumericAction')
    findLoc = re.compile('http://www\.locationary\.com/place/en/US/.{1,50}/.{1,50}/.{1,100}\.jsp')
    findLocL = re.findall(findLoc,s)
    W = []
    X = []
    XA = []
    Y = []
    YA = []
    Z = []
    ZA = []

    for i in range(0,25):
        b = download(findLocL[i])        
        findYP = re.compile('http://www\.yellowpages\.com/')
        findYPL = re.findall(findYP,b)
        findTitle = re.compile('<title>(.*) \(\d{1,10}.{1,100}\)</title>')
        getTitle = re.findall(findTitle,b)        
        findAddress = re.compile('<title>.{1,100}\((.*), .{4,14}, United States\)</title>')
        getAddress = re.findall(findAddress,b)        
        if not findYPL:
            if not getTitle:
                print ""
            else:
                W.append(findLocL[i])
            b = download(findLocL[i])
            if not getTitle:
                print ""
            else:
                X.append(getAddress)
            b = download(findLocL[i])
            if not getTitle:
                print ""
            else:
                Y.append(getTitle)
    sizeWXY = len(W)

    def XReplace(text, dic):
        for i, j in dic.iteritems():
            text = text.replace(i, j)  
        XA.append(text)

    def YReplace(text2, dic2):
        for k, l in dic2.iteritems():
            text2 = text2.replace(k, l)  
        YA.append(text2)

    for d in range(0,sizeWXY):
        old = str(X[d])
        reps = {' ':'-', ',':'', '\'':'', '[':'', ']':''}
        XReplace(old, reps)
        old2 = str(Y[d])
        YReplace(old2, reps)

    count = 0    
    for e in range(0,sizeWXY):
        newYPL = "http://www.yellowpages.com/" + XA[e] + "/" + YA[e] + "?order=distance"
        v = download(newYPL)
        abc = str('<h3 class="business-name fn org">\n<a href="')
        dfe = str('" class="no-tracks url "')
        findFinal = re.compile(abc + '(.*)' + dfe)
        getFinal = re.findall(findFinal, v)
        if not getFinal:
            W.remove(W[(e-count)])
            X.remove(X[(e-count)])
            count = (count+1)
        else:
            for f in range(0,1):
                Z.append(getFinal[f])
    XA = []
    for c in range(0,(len(X))):
        aGd = re.compile('(.*), .{1,50}')
        bGd = re.findall(aGd, str(X[c]))
        XA.append(bGd)
    LenZ = len(Z)
    V = []
    for i in range(0,(len(W))):
        if i == 0:
            countTwo = 0
        gda = download(Z[i-(countTwo)])
        ab = str('"street-address">\n')
        cd = str('\n</span>')
        ZAddress = re.compile(ab + '(.*)' + cd)
        ZAddress2 = re.findall(ZAddress, gda)
        for b in range(0,(len(ZAddress2))):
            if not ZAddress2[b]:
                print ""
            else:
                V.append(str(ZAddress2[b]))
                a = str(W[i-(countTwo)])
                n = str(Z[i-(countTwo)])
                c = str(XA[i])
                d = str(V[i])
                #webbrowser.open(a)
                #webbrowser.open(n)
                m = SequenceMatcher(None, c, d)
                if m.ratio() < 0.50:
                    Z.remove(Z[i-(countTwo)])
                    W.remove(W[i-(countTwo)])
                    countTwo = (countTwo+1)

    def ZReplace(text3, dic3):
        for p, q in dic3.iteritems():
            text3 = text3.replace(p, q)  
        ZA.append(text3)

    for y in range(0,len(Z)):
        old3 = str(Z[y])
        reps2 = {':':'%3A', '/':'%2F', '?':'%3F', '=':'%3D'}
        ZReplace(old3, reps2)
    for z in range(0,len(ZA)):
        findPID = re.compile('\d{5,20}')
        getPID = re.findall(findPID,str(W[z]))
        newPID = re.sub("\D", "", str(getPID))
        finalURL = "http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=" + str(newPID) + "&xxx_c_1_f_987=" + str(ZA[z])
        webbrowser.open(finalURL)
        time.sleep(5)

    os.system("taskkill /F /IM firefox.exe")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:57:22+00:00Added an answer on May 27, 2026 at 3:57 pm

    The first thing to do when a program is slow is to identify bottlenecks; in fact, you want to optimize things that take a long time, not things that may actually be fast. In Python, the most efficient way to do this is with one of the Python profilers, which are dedicated tools for performance analysis. Here is a quickstart:

    python -m cProfile -o prof.dat <prog> <args>
    

    runs your program and stores profiling information in prof.dat. Then,

    python -m pstats prof.dat
    

    runs the profiling information analysis tool pstats. Important pstat commands include:

    sort time
    

    which sorts functions by the time spent in them, and which you can use with a different key instead of time (cumulative,…). Another important command is

    stats
    

    which print statistics (or stats 10 to print the first 10 most time-consuming functions). You can obtain help with ?, or help <command>.

    The way to optimize your program then consists in dealing with the particular code that causes the bottlenecks. You can post the timing results and maybe get some more specific help on the sections of the program that could be most usefully optimized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: it's been ages since I've ran into this "issue" back then, and by
I am trying to do some calculations with python, where I ran out of
What is a good implementation of a IsLeapYear function in VBA? Edit: I ran
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT What small things which are too easy to overlook do I need to
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
-edit- nevermind. I ran the code again and it appears i cant log into
I ran into an interesting (?) problem in the YUI rich edit demo on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.