3. Program running time – Why?
When is the running time (waiting time for user)
noticeable/important?
• web search
• database search
• real-time systems with time constraints
5. Factors that determine
running time of a program
• problem size: n
• basic algorithm / actual processing
• memory access speed
• CPU/processor speed
• # of processors?
• compiler/linker optimization?
6. Running time of a program or transaction
processing time
• amount of input: n min. linear increase
• basic algorithm / actual processing
depends on algorithm!
• memory access speed by a factor
• CPU/processor speed by a factor
• # of processors? yes, if multi-threading or
multiple processes are used.
• compiler/linker optimization? ~20%
7. Running time for a program:
a closer look
time (clock cycles)
CPU memory access disk I/O access
8. Time Complexity
• measure of algorithm efficiency
• has a big impact on running time.
• Big-O notation is used.
• To deal with n items, time complexity can be
O(1), O(log n), O(n), O(n log n), O(n2
), O(n3
),
O(2n
), even O(nn
).
25. Example #1: carry n items
from one room to another room
• How many operations?
• n pick-ups, n forward moves, n drops and n
reverse moves 4 n operations
• 4n operations = c. n = O(c. n) = O(n)
• Similarly, any program that reads n inputs
from the user will have minimum time
complexity O(n).
26. Example #2: Locating patient record in
Doctor Office
What is the time complexity of search?
27. Example #2: Locating patient record in
Doctor Office
What is the time complexity of search?
• Binary Search algorithm at work
• O(log n)
• Sequential search?
• O(n)
28. Example #3: Store manager gives gifts to first
10 customers
• There are n customers in the queue.
• Manager brings one gift at a time.
29. Example #3: Store manager gives gifts to first
10 customers
• There are n customers in the queue.
• Manager brings one gift at a time.
• Time complexity = O(c. 10) = O(1)
• Manager will take exactly same time
irrespective of the line length.
31. Example #4: Thief visits a Doctor
with Back Pain
• Doctor asks a few questions:
– Is there a lot of stress on the job?
– Do you carry heavy weight?
32. Example #4: Thief visits a Doctor
with Back Pain
• Doctor asks a few questions:
– Is there a lot of stress on the job?
– Do you carry heavy weight?
• Doctor says: Never carry > 50 kgs
42. Game console
• Algorithm takes longer to run requires
higher-end CPU to avoid delay to show output
& keep realism.
43. Web server
• Consider 2 web-server algorithms: one takes 5
seconds & another takes 20 seconds.
44. Database access
Since the database load & save operations take
O(n), why bother to optimize database search
operation?
45. Daily data crunching
• Applicable for any industry that collects lot of
data every day.
• Typically takes couple of hours to process.
• What if it takes >1 day?
46. Data crunching pseudocode
• initial setup
• loop
– read one tuple
– open db connection
– send request to db
– get response from db
– close db
• post-processing
47. Data crunching pseudocode
• initial setup
• loop
– read one tuple
– open db connection
– send request to db
– get response from db
– close db
• post-processing
• Equation for running
time = c1. n + d1
• Time complexity is
O(n)
48. Data crunching pseudocode
• initial setup
• open db connection
• loop
– read one tuple
– send request to db
– get response from db
• close db
• post-processing
• Equation for running
time = c2. n + d2
• Time complexity is still
O(n), but the
constants are
different.
• c2 < c1
• d2> d1
50. Summary
• Time complexity is a measure of algorithm efficiency
• Efficient algorithm plays the major role in
determining the running time.
Q: Is it possible to determine running time based on
algorithm’s time complexity alone?
• Minor tweaks in the code can cut down the running
time by a factor too.
• Other items like CPU speed, memory speed, device
I/O speed can help as well.
• For certain problems, it is possible to allocate
additional space & improve time complexity.