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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:58:22+00:00 2026-05-18T19:58:22+00:00

I have been writing command-line Python scripts for a while, but recently I felt

  • 0

I have been writing command-line Python scripts for a while, but recently I felt really frustrated with speed.

I’m not necessarily talking about processing speed, dispatching tasks or other command-line tool-specific processes (that is usually a design/implementation problem), but rather I am talking of simply running a tool to get a help menu, or display minimum information.

As an example, Mercurial is at around 0.080scs and GIT is at 0.030scs

I have looked into Mercurial’s source code (it is Python after all) but the answer to have a fast-responding script still eludes me.

I think imports and how you manage them is a big reason to initial slow downs. But is there a best-practice for fast-acting, fast-responding command line scripts in Python?

A single Python script that import os and optparse and executes main() to parse some argument options takes 0.160scs on my machine just to display the help menu…

This is 5 times slower than just running git!

Edit:

I shouldn’t have mentioned git as it is written in C. But the Mercurial part still stands, and no, pyc don’t feel like big improvement (to me at least).

Edit 2:

Although lazy imports are key to speedups in Mercurial, they key to slowness in
regular Python scripts is not having auto-generated scripts with pkg_resources in them, like:

from pkg_resources import load_entry_point

If you have manually generated scripts that don’t use pkg_resources you should see at least 2x speed increases.

However! Be warned that pkg_resources does provide a nice way of version dependencies so make sure you are aware that not using it basically means possible version conflicts.

  • 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-18T19:58:22+00:00Added an answer on May 18, 2026 at 7:58 pm

    In addition to compiling the Python files, Mercurial modifies importing to be on demand which does indeed reduce the start-up time. It sets __builtin__.__import__ to its own import function in the demandimport module.

    If you look at the hg script in /usr/lib/ (or wherever it is on your machine), you can see this for yourself in the following lines:

    try:
        from mercurial import demandimport; demandimport.enable()
    except ImportError:
        import sys
        sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" %
                         ' '.join(sys.path))
        sys.stderr.write("(check your install and PYTHONPATH)\n")
        sys.exit(-1)
    

    If you change the demandimport line to pass, you will find that the start-up time increases substantially. On my machine, it seems to roughly double.

    I recommend studying demandimport.py to see how to apply a similar technique in your own projects.

    P.S. Git, as I’m sure you know, is written in C so I’m not surprised that it has a fast start-up time.

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

Sidebar

Related Questions

For the last couple of months I have been writing an intranet site for
I understand object oriented programming, and have been writing OO programs for a long
I have been thinking of maybe writing a little tool that logs into SO
I've been writing PHP for about six years now and have got to a
I've been writing PHP web applications for some time, and have come across very
I have just been getting into low level programming (reading/writing to memory that sort
Have been studying the file system related classes of Adobe AIR 1.5, but so
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been writting a keyword search script based on this tutorial: http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/ Like
Have been looking at the MVC storefront and see that IQueryable is returned from

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.