Get Execution Speed of Python Code

Rajendra Dharmkar
Updated on 19-Feb-2020 07:44:46

2K+ Views

To measure time of a program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)OutputThis will give the output −Time elapsed:  0.0009403145040156798You can also use the timeit module to get proper statistical analysis of a code snippet's execution time.  It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows:Exampledef f(x):   return x * x ... Read More

Check if 24 Hours Have Passed Between Datetimes in Python

Rajendra Dharmkar
Updated on 19-Feb-2020 07:44:15

3K+ Views

To find out if 24 hrs have passed between datetimes in Python, you will need to do some date math in Python. So if you have 2 datetime objects, you'll have to subtract them and then take the timedelta object you get as a result and use if for comparision. You can't directly compare it to int, so you'll need to first extract the seconds from it. examplefrom datetime import datetime NUMBER_OF_SECONDS = 86400 # seconds in 24 hours first = datetime(2017, 10, 10) second = datetime(2017, 10, 12) if (first - second).total_seconds() > NUMBER_OF_SECONDS:   print("its been over a day!")OutputThis ... Read More

Create an Object from Class in Java

Prabhas
Updated on 19-Feb-2020 07:39:44

2K+ Views

An object is created from a class using the new keyword. There are three steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.Instantiation − The 'new' keyword is used to create the object.Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.Examplepublic class Sample {    public static void main(String args[]){       Sample s = new Sample();    } }

Compare Python String Formatting with Format

Rajendra Dharmkar
Updated on 19-Feb-2020 07:33:31

260 Views

% can either take a variable or a tuple. So you'd have to be very explicit about what you want it to do. For example, if you try formatting such that −Examplemy_tuple = (1, 2, 3) "My tuple: %s" % my_tuple You'd expect it to give the output: My tuple: (1, 2, 3)OutputBut it will throw a TypeError. To guarantee that it always prints, you'd need to provide it as a single argument tuple as follows −"hi there %s" % (name, )   # supply the single argument as a single-item tupleRemembering such caveats every time is not that easy ... Read More

Declare a Constructor in Java

Sreemaha
Updated on 19-Feb-2020 07:16:09

1K+ Views

While declaring the constructors you should keep the following points in mind.A constructor does not have a return type.The name of the constructor is same as the name of the class. A class can have more than one constructor.Examplepublic class Sample {    int num;    public Sample() {       num = 30;    }    public Sample(int value) {       num = value;    } }

Suspend Calling Thread for Few Seconds in Python

Rajendra Dharmkar
Updated on 19-Feb-2020 07:08:19

430 Views

You can suspend the calling thread for a given time in Python using the sleep method from the time module. It accepts the number of seconds for which you want to put the calling thread on suspension for. Exampleimport time while(True):   print("Prints every 10 seconds")   time.sleep(10)OutputPrints every 10 seconds Prints every 10 seconds Prints every 10 seconds ...Each of the above statements will be printed every 10 seconds.Note that this method also supports floating point values so you can also put the calling thread to sleep for fractions of seconds.

Get Items in Sorted Order from Given Dictionary in Python

Pradeep Elance
Updated on 18-Feb-2020 12:09:13

199 Views

The Python dictionary has key and value pairs. In some situation we will need the items of the dictionary to be sorted according to the keys. In this article we'll see the different ways to get a sorted output from the items in the dictionary.Using Operator ModuleThe Operator module has itemgetter function which can take 0 as the index of input parameter for the keys of the dictionary. We apply the sorted function on top of itemgetter and get the sorted output.Example Live Demodict = {12 : 'Mon', 21 : 'Tue', 17: 'Wed'} import operator print("Given dictionary", str(dict)) print ("sorted order ... Read More

Collapsible Pane in Tkinter Python

Pradeep Elance
Updated on 18-Feb-2020 12:03:15

1K+ Views

Tkinter is the GUI building library of python. In this article we will see how we can create a collapsible pane. They are usefult when we have some large amount of data to be displayed over a GUI canvas but we do not want to be displayed always. It is made collapsible so that it can be displayed as and when needed.The below program creates the collapsible pane where we see the result both after expanding and contracting the arrow. The code comments indicate the approach we take at each step.Examplefrom tkinter import * import tkinter as tk from tkinter ... Read More

Binning Method for Data Smoothing in Python

Pradeep Elance
Updated on 18-Feb-2020 11:57:55

1K+ Views

Many times we use a method called data smoothing to make the data proper and qualitative for statistical analysis. During the smoking process we define a range also called bin and any data value within the range is made to fit into the bin. This is called the binning method. Below is an example of binning. Then we will see how we can achieve the binning method using a Python program.Binning ExampleLet’s take a series of numbers. Find the maximum and minimum values. Decide on the number of bins we need depending on how many data points the analysis needs. ... Read More

ASCII Art Using Python PyFiglet Module

Pradeep Elance
Updated on 18-Feb-2020 11:51:29

2K+ Views

The ASCII text can be used to display many stylish texts by using the module pyfiglet. After installing this module we can use it to control the font that can be used to display the result. In the below program we see various results by choosing various font types.Example# import pyfiglet module import pyfiglet #Text in default font out = pyfiglet.figlet_format("Point") print(out)OutputRunning the above code gives us the following result −Example# import pyfiglet module import pyfiglet #Text in slant font out = pyfiglet.figlet_format("Point", font="slant") print(out)OutputRunning the above code gives us the following result −Example# import pyfiglet module import pyfiglet #Text ... Read More

Advertisements