madcrazydrumma
madcrazydrumma

Reputation: 1897

Differences between imperative and declarative programming languages?

I know what they are:

In an imperative programming language you tell the compiler what you want to happen step by step, whereas in a declarative language you write code which describes the result but not necessarily how to achieve the desired result.

However, I wan't to know the optimisations of using either type of programming language. As well as this - are there any complications involved? For example the need of space/time in running a program developed in either language.

Upvotes: 2

Views: 1384

Answers (1)

Ross Drew
Ross Drew

Reputation: 8246

The Performance Comparison section of Comparison of programming paradigms WikiPedia page pretty much covers what you are asking in a general way.

Purely in terms of total instruction path length, a program coded in an imperative style, without using any subroutines at all, would have the lowest count. However, the binary size of such a program might be larger than the same program coded using subroutines (as in functional and procedural programming) and would reference more "non-local" physical instructions that may increase cache misses and increase instruction fetch overhead in modern processors.

The paradigms that use subroutines extensively (including functional, procedural and object-oriented) and do not also use significant inlining (via compiler optimizations) will, consequently, use a greater percentage of total resources on the subroutine linkages themselves. Object oriented programs that do not deliberately alter program state directly, instead using mutator methods (or "setters") to encapsulate these state changes, will, as a direct consequence, have a greater overhead. This is due to the fact that message passing is essentially a subroutine call, but with three more additional overheads: dynamic memory allocation, parameter copying and dynamic dispatch. Obtaining memory from the heap and copying parameters for message passing may involve significant resources that far exceed those required for the state change itself. Accessors (or "getters") that merely return the values of private member variables also depend upon similar message passing subroutines, instead of using a more direct assignment (or comparison), adding to total path length.

...it goes on

Upvotes: 2

Related Questions