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

  • Home
  • SEARCH
  • 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 9177847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:25:12+00:00 2026-06-17T17:25:12+00:00

I’m trying to time two different functions in python. The first: import cProfile def

  • 0

I’m trying to time two different functions in python.

The first:

import cProfile
def bin_search(A, first,last, target):
#returns index of target in A, if present
#returns -1 if target is not present in A
if first > last:
    return -1
else:
    mid = (first+last)/2
    if A[mid]==target:
        return mid
    elif A[mid]>target:
        return bin_search(A,first,mid-1,target)
    else:
        return bin_search(A,mid+1,last,target)

the second

def trin_search(A,first,last,target):
#returns index of target in A, if present
#returns -1 if target is not present in A
if target> last or target<first:
    return -1
if first>last:
    return -1
else:
    one_third=first+(last-first)/3
    two_thirds=first+2*(last-first)/3
    if A[one_third]==target:
        return one_third
    elif A[one_third]>target:
        #search the left-hand third
        return trin_search(A,first, one_third,target)
    elif A[two_thirds]==target:
        return two_thirds
    elif A[two_thirds]>target:
        #search the middle third
        return trin_search(A,one_third+1,two_thirds-1,target)
    else:
        #search the right-hand third
        return trin_search(A,two_thirds+1,last,target)

I’m trying to time them using the cprofile.run() method. I call:

cprofile.run('trin_search(newlist, newlist[0], newlist[-1], 17)')

and

cprofile.run('bin_search(newlist, newlist[0], newlist[-1], 17)')

with the results for the first:

6 function calls (4 primitive calls) in 0.000 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 :0(setprofile)
    1    0.000    0.000    0.000    0.000 <string>:1(<module>)
  3/1    0.000    0.000    0.000    0.000 Jan 18.py:16(trin_search)
    0    0.000             0.000          profile:0(profiler)
    1    0.000    0.000    0.000    0.000 profile:0(trin_search(newlist, newlist[0], newlist[-1], 17))

and the second

7 function calls (3 primitive calls) in 0.000 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 <string>:1(<module>)
  5/1    0.000    0.000    0.000    0.000 Jan 18.py:2(bin_search)
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}    

How is it possible that they take 0 time to operate?

Cheers,

  • 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-06-17T17:25:14+00:00Added an answer on June 17, 2026 at 5:25 pm

    As already pointed out by other use the timeit module, here’s an example how to time a function with parameters:

    import timeit
    
    arg = 10
    
    def foo(arg):
        return arg**arg
    
    t=timeit.Timer("foo(arg)","from __main__ import foo, arg")
    print t.timeit(5)
    

    Note that you have to import both the function and the variables that you are using in your function call.

    Also, I would suggest that you use IPython where you have “magic commands” so you can simply do %timeit foo(arg).


    For your example, this should work:

    t=timeit.Timer("bin_search(newlist, newlist[0], newlist[-1], 17)",
                           "from __main__ import bin_search, newlist") 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group

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.