You are on page 1of 3

Computer Organization And Assembly Language

Name: M. Roshnab Afraz


Roll No: Fa-2022-BSCS-463
Teacher: Muhammad Daniyal Baig

 Is Switch sltatement always more efficient than sequence of


if-else statement?

No, a switch statement is not always more efficient than a sequence of if-else
statements.

Switch statements can be more efficient when:

 There are a large number of cases to check.


 The cases are all mutually exclusive (i.e., only one case can be true at a time).
 The cases are all known at compile time.

In these cases, the switch statement is more efficient.

If-else statements can be more efficient when:

 There are a small number of cases to check.


 The cases are not all mutually exclusive.
 The cases are not all known at compile time.

In these cases, an if-else statement if more efficient.

 How much Overhead is incurred by a function call?

Overhead in a function is the extra time and memory required to call and return from a
function.
The overhead of a function call in C++ varies depending on a number of factors,

 The number and size of the function arguments.


 Whether the function returns a value.
 Whether the function is virtual or non-virtual.

In general, the overhead of a function call is typically in the order of a few


nanoseconds. However, it can be much higher for functions that take large arguments
or return large values.

 Is a While-Loop more efficient than a For-Loop?

No, While-Loops are not efficient than a For-Loop

This is because for loops have a fixed number of iterations, which the compiler can
use to optimize the code. For example, the compiler can unroll a for loop, which means
that it can copy the code of the loop body into the caller function, and then execute
the loop body directly.

While loops, on the other hand, have an unknown number of iterations. This means
that the compiler cannot perform the same optimizations.

However, there are some cases where a while loop may be more efficient than a for
loop. For example, if you are iterating over a list of unknown size, or if you need to
break out of the loop early, then a while loop may be the better choice.

 Are Pointer refrences more efficient than array indexes?

Pointer references can be more efficient than array indexes in some cases, but not
always.
The main difference between pointer references and array indexes is that pointer
references allow you to access individual elements of an array directly, without having
to go through the array bounds checking that is performed when using array indexes.
This can lead to a performance improvement, especially for large arrays.

However, pointer references also have some disadvantages. First, they are more
difficult to use correctly than array indexes. If you are not careful, you can easily make
a mistake and access an element of the array that is out of bounds, which can lead to
a program crash.

Second, pointer references can make your code more difficult to read and maintain.
This is because they can make it difficult to track where the pointer is pointing and
what data it is referencing.

You might also like