Wednesday, June 13, 2018

Profile your code in Jupyter notebook/lab

We discussed using profiler to profile your code and find out where it is slow in the previous blog, and but you need to run from command line. Today, we will have a look of the profile code in jupyter notebook. Note that, if you haven’t installed ‘line_profiler’, install it first:
pip install line_profiler
Let’s first define some functions to calculate random things. There are three functions that calling one by one.
def square_the_value(x, y):
    
    a = add_1000_times(x, y)
    
    return a**2

def add_1000_times(x, y):
    z = 0
    for i in range(1000):
        z += x
        for j in range(1000):
            z += y
        
    return z

def calculate_my_value(x, y):
    
    a = x + y
    b = x - y
    
    print(square_the_value(a, b))
calculate_my_value(1, 2)
994009000000
Now we want to have an idea of which part of the code running fast and which part running slow. We could use the line_profiler to do the job. First, we need to load the extension:
%load_ext line_profiler
Let’s profile the top level function that we run. We can see that we use ‘%lprun’, which basically run the line_profiler, the ‘-f’ flag is to tell it which function or method we want to profile, and the calculate_my_value(1, 2) is the real statement that we want to run:
%lprun -f calculate_my_value calculate_my_value(1, 2)
994009000000



Timer unit: 1e-06 s

Total time: 0.295409 s
File: <ipython-input-1-0c3fada21717>
Function: calculate_my_value at line 16

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    16                                           def calculate_my_value(x, y):
    17                                               
    18         1          3.0      3.0      0.0      a = x + y
    19         1          1.0      1.0      0.0      b = x - y
    20                                               
    21         1     295405.0 295405.0    100.0      print(square_the_value(a, b))
Now we could see that the line_profiler give us the time to run each line, and what’s the percentage of this line takes. We could see that the last line used all the time. We can continue to profile the last time by entering into the square_the_value function:
%lprun -f square_the_value calculate_my_value(1, 2)
994009000000



Timer unit: 1e-06 s

Total time: 0.39605 s
File: <ipython-input-1-0c3fada21717>
Function: square_the_value at line 1

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           def square_the_value(x, y):
     2                                               
     3         1     396048.0 396048.0    100.0      a = add_1000_times(x, y)
     4                                               
     5         1          2.0      2.0      0.0      return a**2
Similarly, we could profile the add_1000_times function to figure out which line really takes all the time:
%lprun -f add_1000_times calculate_my_value(1, 2)
994009000000



Timer unit: 1e-06 s

Total time: 0.829793 s
File: <ipython-input-1-0c3fada21717>
Function: add_1000_times at line 7

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                           def add_1000_times(x, y):
     8         1          1.0      1.0      0.0      z = 0
     9      1001        382.0      0.4      0.0      for i in range(1000):
    10      1000        423.0      0.4      0.1          z += x
    11   1001000     388197.0      0.4     46.8          for j in range(1000):
    12   1000000     440788.0      0.4     53.1              z += y
    13                                                   
    14         1          2.0      2.0      0.0      return z
The profiler is really useful, I use it all the time to optimize my code to remove some of the inefficient code. Hope you will find it useful as well.

9 comments:

  1. Replies
    1. I was able to win back my Ex lover with the help of…. Dr.mac@yahoo.com..

      Delete
  2. Am here to testify what this great spell caster done for me. i never believe in spell casting, until when i was was tempted to try it. i and my wife have been having a lot of problem living together, she will always not make me happy because she have fallen in love with another man outside our relationship, i tried my best to make sure that my wife leave this woman but the more i talk to her the more she makes me fell sad, so my marriage is now leading to divorce because she no longer gives me attention. so with all this pain and agony, i decided to contact this spell caster to see if things can work out between me and my wife again. this spell caster who was a man told me that my wife is really under a great spell that she have been charm by some magic, so he told me that he was going to make all things normal back. he did the spell on my wife and after 5 days my wife changed completely she even apologize with the way she treated me that she was not her self, i really thank this man his name is Dr ose he have bring back my wife back to me i want you all to contact him who are having any problem related to marriage issue and relationship problem he will solve it for you. his email is oseremenspelltemple@gmail.com he is a man and his great. wish you good time.
    He cast spells for different purposes like
    (1) If you want your ex back.
    (2) if you always have bad dream
    (3) You want to be promoted in your office.
    (4) You want women/men to run after you.
    (5) If you want a child.
    (6) You want to be rich.
    (7) You want to tie your husband/wife to be yours forever.
    (8) If you need financial assistance.
    (9) HIV/AIDS CURE
    (10) is the only answer to that your problem of winning the lottery

    Contact him today on oseremenspelltemple@gmail.com or whatsapp him on +2348136482342

    ReplyDelete
  3. Five weeks ago my boyfriend broke up with me. It all started when i went to summer camp i was trying to contact him but it was not going through. So when I came back from camp I saw him with a young lady kissing in his bed room, I was frustrated and it gave me a sleepless night. I thought he will come back to apologies but he didn't come for almost three week i was really hurt but i thank Dr.Azuka for all he did i met Dr.Azuka during my search at the internet i decided to contact him on his email dr.azukasolutionhome@gmail.com he brought my boyfriend back to me just within 48 hours i am really happy. What’s app contact : +44 7520 636249‬

    ReplyDelete
  4. My name is Tom cam!!! i am very grateful sharing this great testimonies with you all, The best thing that has ever happened in my life, is how i worn the Powerball lottery. I do believe that someday i will win the Powerball lottery. finally my dreams came through when i contacted Dr. OSE and tell him i needed the lottery winning special numbers cause i have come a long way spending money on ticket just to make sure i win. But i never knew that winning was so easy with the help of Dr. OSE, until the day i meant the spell caster testimony online, which a lot of people has talked about that he is very powerful and has great powers in casting lottery spell, so i decided to give it a try. I emailed Dr. OSE and he did a spell and gave me the winning lottery special numbers 62, and co-incidentally I have be playing this same number for the past 23years without any winning, But believe me when I play the special number 62 this time and the draws were out i was the mega winner because the special 62 matched all five white-ball numbers as well as the Powerball, in the April 4 drawing to win the $70 million jackpot prize...… Dr. OSE, truly you are the best, with Dr. OSE you can will millions of money through lottery. i am a living testimony and so very happy i meant him, and i will forever be grateful to him...… you can Email him for your own winning special lottery numbers now oseremenspelltemple@gmail.com OR WHATSAPP him +2348136482342

    ReplyDelete
  5. Hello everyone I want to let the whole world know how Dr Great has restored my broken relationship with Wayne. We have been together for 3 years and he told me that he doesn’t love me like he used to. Things have not been good for about 4 months and he ended this about 2 weeks ago. I was miserable and just don’t want to go on anymore. I did text him right after this but he didn’t even respond to me. One day I was searching for a way to get him back on the Internet when I saw a post of a lady testifying of how a love spell caster called Dr Great helped her to get back her Ex, I was so desperate to get mine back so I messaged him and explained my situation to him and he instructed me to do somethings which I did and 24 hours later my boyfriend Wayne came back kneeling and begging for my forgiveness and I forgive him and our relationship was back to normal again, if your Ex broke up with you or you have any problem then you problem has come to an end because Dr Great can solve all marriage and relationship problems. If you need his urgent help email him at

    infinitylovespell@gmail.com OR infinitylovespell@yahoo.com

    You can also call or add him on WhatsApp +2348118829899

    BLOG http://infinitylovespell1.blogspot.com




    Hello everyone I want to let the whole world know how Dr Great has restored my broken relationship with Wayne. We have been together for 3 years and he told me that he doesn’t love me like he used to. Things have not been good for about 4 months and he ended this about 2 weeks ago. I was miserable and just don’t want to go on anymore. I did text him right after this but he didn’t even respond to me. One day I was searching for a way to get him back on the Internet when I saw a post of a lady testifying of how a love spell caster called Dr Great helped her to get back her Ex, I was so desperate to get mine back so I messaged him and explained my situation to him and he instructed me to do somethings which I did and 24 hours later my boyfriend Wayne came back kneeling and begging for my forgiveness and I forgive him and our relationship was back to normal again, if your Ex broke up with you or you have any problem then you problem has come to an end because Dr Great can solve all marriage and relationship problems. If you need his urgent help email him at

    infinitylovespell@gmail.com OR infinitylovespell@yahoo.com

    You can also call or add him on WhatsApp +2348118829899

    BLOG http://infinitylovespell1.blogspot.com

    ReplyDelete
  6. DR EMU WHO HELP PEOPLE IN ANY TYPE OF LOTTERY NUMBERS

    It is a very hard situation when playing the lottery and never won, or keep winning low fund not up to 100 bucks, i have been a victim of such a tough life, the biggest fund i have ever won was 100 bucks, and i have been playing lottery for almost 12 years now, things suddenly change the moment i came across a secret online, a testimony of a spell caster called DR EMU, who help people in any type of lottery numbers, i was not easily convinced, but i decided to give try, now i am a proud lottery winner with the help of DR EMU, i won $1,000.0000.00 and i am making this known to every one out there who have been trying all day to win the lottery, believe me this is the only way to win the lottery.

    Contact him via email Emutemple@gmail.com
    What's app +2347012841542
    Https://emutemple.wordpress.com/

    ReplyDelete
  7. I was diagnosed of herpes 3 years, and ever since then i have been taking treatment to prevent outbreaks, burning and blisters, but there was no improvement until i came across testimonies of Dr. VOODOO on how he has been curing different people from different diseases all over the world, then i contacted him. After our conversation he sent me the medicine which i took according to his instructions for up to 2 weeks. After completing the medication i went back to my doctor for another test and the virus was all gone and i was completely cured, since then i have not had any signs of outbreak. I'm so filled with joy. With herbal medication Herpes Virus is 100% curable. I refer Dr VOODOO to everyone out there with the virus Email: voodoospelltemple66@gmail.com Add Dr voodoo on whatsApp +2348140120719

    ReplyDelete
  8. It was the world crushing down on me, I was almost loosing it all until I stumbled on Dr Ogudugu on YouTube. He helped me with the herbal remedy that set me free from the cruel hand of the nasty virus known as herpes simplex virus(HSV). He gave me my life back and it like a new life.

    ReplyDelete