Is lazy evaluation efficient/optimizable?

2020-08-26T12:04:58

I've found many uses for lazy evaluation, such as a tool for optimization (e.g. matrices).

Another use is syntactic sugar. But before I go overboard and make my code look at lot cleaner at the cost of runtime overhead, do compilers know how to optimize this kind of stuff? Or should I just only use it when the potential overhead is faster than not using lazy evaluation?

Following is an example of what I mean. It's not my actual use case, just a simple version of non-lazy vs lazy eval.

// original
template < class out,
           class in >
out && lazy_cast( in && i )
{
    return ( out && )( i );
}

// usage
char c1 = 10;
int c2 = lazy_cast< int >( c1 );
// lazy
template < class in >
class C_lazy_cast
{
public:
    in && i;

    template < class out && >
    operator out &&( )
    {
        return ( out && )( i );
    }
};

template < class in >
C_lazy_cast< in > lazy_cast( in && i )
{
    return { std::forward< in >( i ) };
}

// usage
char c1 = 10;
int c2 = lazy_cast( c1 );

For the sake of completeness, information about MSVC, GCC, and clang should be enough.

Copyright License:
Author:「j__」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/63590336/is-lazy-evaluation-efficient-optimizable

About “Is lazy evaluation efficient/optimizable?” questions

I've found many uses for lazy evaluation, such as a tool for optimization (e.g. matrices). Another use is syntactic sugar. But before I go overboard and make my code look at lot cleaner at the cost...
I recently installed Ruby 2.0.0 and found that it now has a lazy method for the Enumerable mixin. From previous experience in functional languages, I know that this makes for more efficient code. ...
I understand what lazy evaluation is, and how it works and the advantages it has, but could you explain me what strict evaluation really is in Haskell? I can't seem to find much info about it, sinc...
I am trying to learn Haskell, but i am stuck in understanding lazy evaluation. Can someone explain me lazy evaluation in detail and the output of the following 2 cases[with explaination] in relat...
What are the semantics of Java lazy evaluation? Do there exist triggers, stored together with assignment stores that append a trigger to the semantic stack of the trigger store if a program contain...
What advantages are there to Lazy Evaluation as opposed to Eager Evaluation? What performance overhead is there? Is Lazy Evaluation going to be slower or faster? Why(or does it depend on implement...
Recently I decided to give a try and started to read book "Fast Data Processing Systems with SMACK stack" by Raul Estrada. After 2 first chapters I thought that it is not-so-bad compilation of "hello
x_ok &lt;- function(x) { !is.null(x) &amp;&amp; length(x) == 1 &amp;&amp; x &gt; 0 } x_ok(1) #&gt; [1] TRUE x_ok(1:3) #&gt; [1] FALSE The desired behaviour is reached by combining the a
What is lazy evaluation in Python? One website said : In Python 3.x the range() function returns a special range object which computes elements of the list on demand (lazy or deferred evaluation...
Is ternary operator an example of lazy evaluation? If we assume a code snippet like: Variable = Condition ? function1(param) : function2(param); Can it be an example of lazy evaluation?

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.