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 6359053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:28:07+00:00 2026-05-24T23:28:07+00:00

I’ve been working on one of the coding challenges on InterviewStreet.com and I’ve run

  • 0

I’ve been working on one of the coding challenges on InterviewStreet.com and I’ve run into a bit of an efficiency problem. Can anyone suggest where I might change the code to make it faster and more efficient?

Here’s the code

Here’s the problem statement if you’re interested

  • 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-24T23:28:07+00:00Added an answer on May 24, 2026 at 11:28 pm

    If your question is about optimising python code generally (which I think it should be 😉 then there are all sorts of intesting things you can do, but first:

    You probably shouldn’t be obsessively optimising python code! If you’re using the fastest algorithm for the problem you’re trying to solve and python doesn’t do it fast enough you should probably be using a different language.

    That said, there are several approaches you can take (because sometimes, you really do want to make python code faster):

    Profile (do this first!)

    There are lots of ways of profiling python code, but there are two that I’ll mention: cProfile (or profile) module, and PyCallGraph.

    cProfile

    This is what you should actually use, though interpreting the results can be a bit daunting.
    It works by recording when each function is entered or exited, and what the calling function was (and tracking exceptions).

    You can run a function in cProfile like this:

    import cProfile
    cProfile.run('myFunction()', 'myFunction.profile')
    

    Then to view the results:

    import pstats
    stats = pstats.Stats('myFunction.profile')
    stats.strip_dirs().sort_stats('time').print_stats()
    

    This will show you in which functions most of the time is spent.

    PyCallGraph

    PyCallGraph provides a prettiest and maybe the easiest way of profiling python programs — and it’s a good introduction to understanding where the time in your program is spent, however it adds significant execution overhead

    To run pycallgraph:

    pycallgraph graphviz ./myprogram.py
    

    Simple! You get a png graph image as output (perhaps after a while…)

    Use Libraries

    If you’re trying to do something in python that a module already exists for (maybe even in the standard library), then use that module instead!

    Most of the standard library modules are written in C, and they will execute hundreds of times faster than equivilent python implementations of, say, bisection search.

    Make the Interpreter do as Much of Your Work as You Can

    The interpreter will do some things for you, like looping. Really? Yes! You can use the map, reduce, and filter keywords to significantly speed up tight loops:

    consider:

    for x in xrange(0, 100):
        doSomethingWithX(x)
    

    vs:

    map(doSomethingWithX, xrange(0,100))
    

    Well obviously this could be faster because the interpreter only has to deal with a single statement, rather than two, but that’s a bit vague… in fact, this is faster for two reasons:

    • all flow control (have we finished looping yet…) is done in the interpreter
    • the doSomethingWithX function name is only resolved once

    In the for loop, each time around the loop python has to check exactly where the doSomethingWithX function is! even with cacheing this is a bit of an overhead.

    Remember that Python is an Interpreted Language

    (Note that this section really is about tiny tiny optimisations that you shouldn’t let affect your normal, readable coding style!)
    If you come from a background of a programming in a compiled language, like c or Fortran, then some things about the performance of different python statements might be surprising:

    try:ing is cheap, ifing is expensive

    If you have code like this:

    if somethingcrazy_happened:
         uhOhBetterDoSomething()
    else:
         doWhatWeNormallyDo()
    

    And doWhatWeNormallyDo() would throw an exception if something crazy had happened, then it would be faster to arrange your code like this:

    try:
        doWhatWeNormallyDo()
    except SomethingCrazy:
        uhOhBetterDoSomething()
    

    Why? well the interpreter can dive straight in and start doing what you normally do; in the first case the interpreter has to do a symbol look up each time the if statement is executed, because the name could refer to something different since the last time the statement was executed! (And a name lookup, especially if somethingcrazy_happened is global can be nontrivial).

    You mean Who??

    Because of cost of name lookups it can also be better to cache global values within functions, and bake-in simple boolean tests into functions like this:

    Unoptimised function:

    def foo():
        if condition_that_rarely_changes:
             doSomething()
        else:
             doSomethingElse()
    

    Optimised approach, instead of using a variable, exploit the fact that the interpreter is doing a name lookup on the function anyway!

    When the condition becomes true:

    foo = doSomething # now foo() calls doSomething()
    

    When the condition becomes false:

    foo = doSomethingElse # now foo() calls doSomethingElse()
    

    PyPy

    PyPy is a python implementation written in python. Surely that means it will run code infinitely slower? Well, no. PyPy actually uses a Just-In-Time compiler (JIT) to run python programs.

    If you don’t use any external libraries (or the ones you do use are compatible with PyPy), then this is an extremely easy way to (almost certainly) speed up repetitive tasks in your program.

    Basically the JIT can generate code that will do what the python interpreter would, but much faster, since it is generated for a single case, rather than having to deal with every possible legal python expression.

    Where to look Next

    Of course, the first place you should have looked was to improve your algorithms and data structures, and to consider things like caching, or even whether you need to be doing so much in the first place, but anyway:

    • This page of the python.org wiki provides lots of information about how to speed up python code, though some of it is a bit out of date.

    • Here’s the BDFL himself on the subject of optimising loops.

    There are quite a few things, even from my own limited experience that I’ve missed out, but this answer was long enough already!

    This is all based on my own recent experiences with some python code that just wasn’t fast enough, and I’d like to stress again that I don’t really think any of what I’ve suggested is actually a good idea, sometimes though, you have to….

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.