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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:29:56+00:00 2026-05-12T17:29:56+00:00

Apologies for reposting but I had to edit this question when I got to

  • 0

Apologies for reposting but I had to edit this question when I got to work and realized I needed to have an account to do so. So here it goes again (with a little more context).

I’m trying to time how long a script takes to execute, and I am thinking of doing that by checking the elapsed time after every line of code is executed. I’ve done this before when the script has contained method definitions, but am not sure how it would work in this instance.

So my question is: Is there a way to use the setTrace() function in a script that has no method definitions? i.e.

for i in range(1, 100):
    print i

def traceit(frame, event, arg):
    if event == "line":
        lineno = frame.f_lineno
        print "line", lineno

    return traceit

sys.settrace(traceit)
  • 1 1 Answer
  • 1 View
  • 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-12T17:29:56+00:00Added an answer on May 12, 2026 at 5:29 pm

    No, as the docs say, “The trace function is invoked (with event set to ‘call’) whenever a new local scope is entered” — if you never enter a local scope (and only execute in global scope), the trace function will never be called. Note that settrace is too invasive anyway for the purpose of timing “how long a script takes to execute” as it will alter what it’s measuring too much; if what you say is actually what you want, just take the time at start of execution and register with atexit a function that gets the time again and prints the difference. If what you want is different, i.e., profiling, see cProfile .

    Further note that the code example you give couldn’t possibly do anything useful (even though I’ve edited it to fix an indent error): first it loops, then it defs a function, finally it calls settrace… then immediately ends because there’s no more code after that! If you want anything to happen before that loop start, and you want to have everything at module top level (bad idea, but, whatever), you have to put the “anything” lexically before the loop, not after…;-)

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

Sidebar

Related Questions

Apologies in advance for the long-winded question. I'm really a database programmer, but have
Apologies for a simple question, but I'm very new to Perl! I have an
Apologies if this is a simple question, but I turn to the wisdom of
Apologies for the massively stupid question but I am curious how to get this
Apologies in advance for the length of this question! I have a data structure
Apologies if I am not wording this correctly but what I have at the
I have seen this question answered in reference to Bash, but can't find one
Apologies if this question has already been asked but I do not think I
Apologies if this is a too basic question but I couldn't find any satisfactory
Apologies for the vagueness of the question here but can someone point me to

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.