2. Instruction space
• Space needed to store the compiled version of
the program instructions.
• The compiler used to compile the program
into machine code
– Suppose we have an expression a+b+c*d, the
compiler computes this as (c*d)+a+b and generate
the shorter and more time-efficient code.
3. Data Space
• The space needed to store all the constants
and variables.
• Data space has two components
1. Space needed by constants and simple variables.
2. Space needed by dynamically allocated objects such
as arrays and class instances
4. Environment Stack Space
• The environment stack is used to save information
needed to resume execution of partially completed
functions and methods.
• Beginning performance analysts often ignore the
space needed by the environment stack because
they don’t understand how functions are invoked
and what happens on termination.
• Each time a function is invoked the following data
are saved on the environment stack:
– The return address
– The values of all local variables and formal parameters in
the functions being invoked (necessary for recursive
functions only).
6. Examples
int main()
{ int i, sum=0;
int n;
cin>>n;
for (i=0; i<n; i++)
sum+=i;
}
Time Complexity - O(n)
Space complexity – O(1)
7. Examples
int main()
{ int n;
cin>>n;
int *arr;
arr=new int [n];
for (int i=0; i<n; i++)
for (int j=0; j<i; j++)
{
some statements;
}
return 0;
}}
Time Complexity - O(n2)
Space complexity – O(n)
8. const int n=100;
int main()
{
for (int i=0;i<n;i++)
f();
return 0;
}
void f()
{ int a[n];
for (j=0;j<n;j++)
{
some statements;
}
}
Time Complexity - O(n2)
Space complexity – O(n)
9. int n=64;
steps=0;
for (int i=1; i<=n;i*=2)
steps++;
cout<<steps;
int* A;
A=new int[steps];
….
….
Time Complexity - O(log n)
Space complexity – O(log n)