You are on page 1of 14

CLASS-XII

COMPUTER SCIENCE

PYTHON LIBRARIES AND IDEA OF EFFICIENCY

NOTES

What is a Library ? :A Library refers to a collection of modules that together


cater to specific type of needs or applications. Here we will discuss some python
libraries as well as creating own libraries and modules.

What is a module? : The act of partitioning a program into individual components


is called as modularity. A module is a separate unit in itself. The advantages of
module are:

 It reduces the complexity to some degree.


 It creates a number of well defined, documented boundaries within the
program.

What is a package ? : Python packages are basically collection of modules and


sub packages under common namespaces. It contains a special file such as
__init__.py.The namespace is created by a directory that contains all related
modules.

What is a framework ? : A Web framework is a collection of packages or


modules which allow developers to write Web application or services without
having to handle such low-level details as protocols, sockets or process/thread
management.
Let’s discuss all these terms in detail:

1. Module

2. Package

3. Library

4. Framework

1.Using Module-

It is a file which contains python functions/global variables/classes etc. It is just


.py file which has python executable code/statement . For example: Let’s create a
file user module.py

def hellomessage(user_name):
return “Hello"+name

Now we can import user module.py module either in python interpreter or other
pyfile.

import usermodule

print usermodule.hellomessage(“India")

How to import modules in Python?

Python module can be accessed in any of following way.

1. Python import statement

import math

print(“2 to the power 3 is ", math.pow(2,3))

Just similar to math, user defined module can be accessed using import
statement

2. Import with renaming

import math as mt

print(“2 to the power 3 is ", mt.pow(2,3))

3. Python from...import statement

from math import pow

print(“2 to the power 3 is ", pow(2,3))

4. Import all names

from math import *

print(“2 to the power 3 is ", pow(2,3))


2.Using Package-

It is name space that contains multiple package or modules. It is a

directory which contains a special file __init__.py Let’s create a directory


geometry. Now this package contains multiple packages/modules to handle user
related requests.

geometry/ # top level package

__init__.py

rectangle/#first sub package

__init__.py

area_rect.py

perimeter_rect.py

circle/#second sub package

__init__.py

area_circ.py

perimeter_circ.py

Now we can import it in following way in other .pyfile

from geometry.rectangle import area_rect

from geometry.circleimport perimeter_circ

3. Using Library

It is a collection of various packages. Conceptually, There is no difference between


package and python library. In Python, a library is used loosely to describe a
collection of the core modules. ‘standard library’ of Python language comes
bundled with the core Python distribution are collection of exact syntax, token and
semantics of the Python language. The python standard library lists down approx.
more than 200 such core modules that form the core of Python. “Additional
libraries” refer to those optional components that are commonly included in Python
distributions. The Python installers automatically adds the standard library and
some additional libraries. The additional library is generally provided as a
collection of packages. To use such additional library we have to use packaging
tools like easy install or pip to install such additional libraries.

4.Using Framework : Framework is like a collection of various libraries which


architects some more component. For e.g. Django which has various in-built
libraries like Auth, user, database connector etc.

Idea of Efficiency –Algorithm

Efficient programming is a manner of programming that, when the program is


executed, it uses a low amount of overall resources pertaining to specially
computer hardware. A program is designed by a human being, and different human
beings may use different algorithms, or sequences of codes, to perform particular
tasks, so the efficiency of such different programs/algorithm varies, depend upon
the number of resources being used. Practicing to create a low size (number of line
of codes/number of operations) and low resource algorithm results in an efficient
program.
What is an Algorithm?
•An algorithm is a step-by-step procedure for solving a problem in a finite amount
of time.
•The word algorithm comes from the name of a Persian Mathematician Abu
Ja’farMohammed ibn-I Musa al Khowarizmi.
•For a given problem:
There can be more than one solution (more than one algorithm) .
An algorithm can be implemented using different programming languages on
different platforms.

Designing of an Algorithm:
•Design of an algorithm is an area of computer science which minimizes the cost.
•Always design algorithms which minimize the cost.
•Analysis of Algorithms is the area of computer science that provides tools to
analyze the efficiency of different methods of solutions.
•In short predict the cost of an algorithm in terms of resources and performance is
called analysis of Algorithm.

Properties of Algorithm
Donald Ervin Knuth has given a list of five properties for an algorithm, these
properties are:
1.FINITENES
2.DEFINITENESS
3.INPUT
4.OUTPUT
5.EFFECTIVENESS

FINITENESS:
An algorithm must always terminate after a finite number of steps.
It means after every step one reach closer to solution of the problem and after a
finite number of steps algorithm reaches to an end point.

DEFINITENESS
Each step of an algorithm must be precisely defined.
It is done by well thought actions to be performed at each step of the algorithm.
Also the actions are defined unambiguously for each activity in the algorithm.

INPUT
Any operation you perform need some beginning value/ quantities associated
with different activities in the operation.
So the value/quantities are given to the algorithm before it begins.

OUTPUT
One always expects output/result (expected value/quantities) in terms of output
from an algorithm.
The result may be obtained at different stages of the algorithm. Result is
obtained from the intermediate stage of the operation then it is known as
intermediate result.
Result obtained at the end of algorithm is known as end result.
The output is expected value/quantities always have a specified relation to the
inputs.
EFFECTIVENESS
Algorithms to be developed/written using basic operations. Algorithms
operations should be done exactly and in a finite amount of time by a person, by
using paper and pencil only.

Performance of an algorithm depends on many factors

Performance defined as inversely proportional to the wall clock time:-

Wall clock time/elapsed time: time to complete a task as seen by the user. In wall
clock timing all kind of time is included, e.g. operating system over head or
potentially interfering other applications etc.

CPU time :does not include time slices introduced by external sources(e.g.
running other applications).

Performance defined as inversely proportional to the wall clock time:-


To maximize performance, minimize execution time
performance=1/execution_time X

“X is n times faster than Y”–Execution time on Y is n times longer than on X


Performance x Execution time y
-------------------- = --------------------- = n
Performance y Execution time x

Performance defined as inversely proportional to the wall clock time:-

E.g. If a particular desk top runs a program in 60 second and a laptop runs the same
program in 90 seconds how much faster is the desktop than the laptop?
=Performance desktop/Performance laptop
=(1/60)/(1/90)
=1.5 So ,the desktop is 1.5 times faster than the laptop

Count the number of operations a piece of code is performing:-

To compute the number of operations in a piece of code , then simply count the
number of arithmetic operations + other operation that code is performing. All
operations (addition ,subtraction ,multiplication ,and division) are usually counted
to be the same, which is not exactly true, since multiplication includes several
additions and division includes several multiplication when actually executed by a
computer. However, we are looking for an estimate here, so it is reasonable to
assume that on average, all operations count in the same manner .Here is an
example(just for illustration):
r=0
for i in range(4):
for n in range(4):
r=r+(i*n)
print(r)
For each r there is 1 multiplications,1 addition and 1assignment resulting in 3
operations.This loop is executed 4X4times ,so there are(4X4)r operations.This is
the the order of the code.In this example,its is O(42r).
Measure the time taken by a Python Program

To measure the script execution time is simply possible by using time built-in
Python module time() function is used to count the numbe of seconds elapsed since
the epoch .e.g.program
import time start=time.time()
r=0
for i in range(400):
for n in range(400):
r=r+(i*n)
print(r)
end = time.time()
print(end-start)
OUTPUT6368040000
0.12480020523071289#TIMETAKETOEXECUTETHEPYTHONSCRIPT

Compare programs for time efficiency

With the help of time() function, we can compare two/more programs with
different algorithm for same problem that which one take less time. Below code
scripts are for prime no time efficiency purpose.
•Internal Factors: Specify algorithm’s efficiency in terms of
Time required to run
Space (Memory) required to run

•External Factors: affect the algorithm’s performance


Size of the input to the algorithm
Speed of computer on which it is run
Quality of the Computer Efficiency of Algorithm

There are two aspects of algorithm performance or efficiency:

Internal Factors :

•TIME COMPLEXITY: It is essentially efficiency ,or how long a program


function takes to process a given input.
Instructions take time.
How fast does the algorithm perform?
What affects its runtime?

•SPACE COMPLEXITY: of an algorithm is total space taken by the algorithm


with respect to the input size .Space complexity includes both Auxiliary space and
space used by input.
Data structure stake space
What kind of data structures can be used?
How does choice of data structure affect the runtime?

Asymptotic Analysis:
•In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of
input size
•Asymptotic analysis of an algorithm refers to defining the mathematical framing
of its run-time performance.
•Usually, the time required by an algorithm falls under three types Best Case −
Minimum time required for program execution.
Average Case − Average time required for program execution.
Worst Case − Maximum time required for program execution
Asymptotic Analysis:
•Following are the commonly used asymptotic notations to calculate the running
time complexity of an algorithm.

Asymptotic Analysis:
•Following graph is commonly used to calculate the running time complexity of an
algorithm.
Ο Notation ( Big Oh Notation)
•Big O specifically describes the worst-case scenario, and can be used to describe
the execution time required or the space used (e.g. in memory or on disk) by an
algorithm.
•Big O complexity can be visualized with this graph:

•Big Ω describes the set of all algorithms that run no better than a certain
speed (it’s a lower bound)
•It measures the best case time complexity or the best amount of time an algorithm
can possibly take to complete.
•Best case performance of an algorithm given function g(n), we denote by Ω(g(n))
the set of functions.Ω (g(n)) = {f(n): there exist positive constants c and n0 such
that 0 <= c*g(n) <= f(n) for all n >= n0}.
•Best case performance of an algorithm is generally not useful , the Omega
notation is the least used notation among all

O(θ notation)
•You can use the big-Theta notation to describe the average-case complexity.
•The θ notation describes asymptotic tight bounds
TIME COMPLEXITY OF SORTING ALGORITHMSTIME COMPLEXITY OF
SEARCHING ALGORITHM

SPACE COMPLEXITY SPACE COMPLEXITY OF SEARCHING AND


SORTING ALGORITHMS

The worst-case complexity of the algorithm is the function defined by the


maximum number of steps taken on any instance of size n.
The best-case complexity of the algorithm is the function defined by the
minimum number of steps taken on any instance of size n.
Finally, the average-case complexity of the algorithm is the function defined
by the average number of steps taken on any instance of size n.

You might also like