Deferred execution and eager evaluation
-
Hi Could you please give me an example for Deferred execution with eager evaluation in C#? I read from MSDN that deferred execution in LINQ can be implemented either with lazy or eager evaluation...i could find examples in the internet for Deferred execution with lazy evaluation ,however i could not find any example for Deferred execution with eager evaluation....please help me....its urgent...
-
Hi Could you please give me an example for Deferred execution with eager evaluation in C#? I read from MSDN that deferred execution in LINQ can be implemented either with lazy or eager evaluation...i could find examples in the internet for Deferred execution with lazy evaluation ,however i could not find any example for Deferred execution with eager evaluation....please help me....its urgent...
Lazy evaluation[^]: the value of an expression is not evaluated until it is needed. Eager evaluation[^]: the value of an expression is evaluated as soon as it gets bound to a variable.
var arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var elementsGreaterThan5Lazy = from n in arr
where n > 5
select n; // the query doesn't execute here. If you have a million elements in the array you're not filtering them at this point of the program.var elementsGreaterThan5Eager = (from n in arr
where n > 5
select n).ToArray(); // the query does execute here because you're forcing the execution by creating an array of the results (you want those values > 5 now in an array).The first expression actually evaluates to an iterator (an object that is to perform the desired operation) not the actual result of the operation (the elements which are greater than 5). In the second expression, the ToArray method is forcing the execution of the query to get the elements as an array. In other words, you want an actual array now so the query has to be executed to get the array. Let's say you then write something like
var firstElementGreaterThan5 = elementsGreaterThan5Lazy.First();
Here you're forcing the query to execute to get the first element that is greater than 5 (and to throw an exception when the sequence is empty). Actually, you're not looping over all the elements, you're just looping until you find the desired value. In other words, if you have a million elements in the array and the first element greater than 5 is in the second location, you're actually doing 2 iterations and not iterating over the whole million elements. Some extension methods like ToArray, ToList, First, Last... do force the execution of the query. While other methods like Where, Take, Skip... do not execute the query but return an iterator. You can tell whether a method forces the execution or not from its name or from the documentation. I hope that answers your question.
Eslam Afifi
-
Lazy evaluation[^]: the value of an expression is not evaluated until it is needed. Eager evaluation[^]: the value of an expression is evaluated as soon as it gets bound to a variable.
var arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var elementsGreaterThan5Lazy = from n in arr
where n > 5
select n; // the query doesn't execute here. If you have a million elements in the array you're not filtering them at this point of the program.var elementsGreaterThan5Eager = (from n in arr
where n > 5
select n).ToArray(); // the query does execute here because you're forcing the execution by creating an array of the results (you want those values > 5 now in an array).The first expression actually evaluates to an iterator (an object that is to perform the desired operation) not the actual result of the operation (the elements which are greater than 5). In the second expression, the ToArray method is forcing the execution of the query to get the elements as an array. In other words, you want an actual array now so the query has to be executed to get the array. Let's say you then write something like
var firstElementGreaterThan5 = elementsGreaterThan5Lazy.First();
Here you're forcing the query to execute to get the first element that is greater than 5 (and to throw an exception when the sequence is empty). Actually, you're not looping over all the elements, you're just looping until you find the desired value. In other words, if you have a million elements in the array and the first element greater than 5 is in the second location, you're actually doing 2 iterations and not iterating over the whole million elements. Some extension methods like ToArray, ToList, First, Last... do force the execution of the query. While other methods like Where, Take, Skip... do not execute the query but return an iterator. You can tell whether a method forces the execution or not from its name or from the documentation. I hope that answers your question.
Eslam Afifi
Dear Eslam Afifi, Thanks a lot for your explanation.You have explained the difference between lazy and eager evalation.I could find lot of explanation on the same topic in text books and in the internet.However my question is how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution.Could you please provied any example for that? Moreover,how deferred execution differs from lazy evaluation?In my point of view,both are looking same.Could you please provide any example for this too? Thanks in Advance...!
-
Dear Eslam Afifi, Thanks a lot for your explanation.You have explained the difference between lazy and eager evalation.I could find lot of explanation on the same topic in text books and in the internet.However my question is how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution.Could you please provied any example for that? Moreover,how deferred execution differs from lazy evaluation?In my point of view,both are looking same.Could you please provide any example for this too? Thanks in Advance...!
mbabube wrote:
how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution
Eslam Afifi wrote:
var elementsGreaterThan5Eager = (from n in arr where n > 5
select n).ToArray();The query is deferred, using the ToArray method forces the execution of the query making it eager evaluation (at this point of the program you're looping over all the elements selecting those which match the criteria and putting them in an array). The eager evaluation of ToArray employs (uses) the deferred execution of the query. About the difference between deferred execution and lazy evaluation, the way I see it is that lazy evaluation gives you all the result value when you need it while deferred execution is executing only the part you need from an expression/query (not necessarily all of it) to evaluate another expression/query. I hope I understood your question right and that I gave you a good answer. And sorry for the late reply.
Eslam Afifi
-
mbabube wrote:
how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution
Eslam Afifi wrote:
var elementsGreaterThan5Eager = (from n in arr where n > 5
select n).ToArray();The query is deferred, using the ToArray method forces the execution of the query making it eager evaluation (at this point of the program you're looping over all the elements selecting those which match the criteria and putting them in an array). The eager evaluation of ToArray employs (uses) the deferred execution of the query. About the difference between deferred execution and lazy evaluation, the way I see it is that lazy evaluation gives you all the result value when you need it while deferred execution is executing only the part you need from an expression/query (not necessarily all of it) to evaluate another expression/query. I hope I understood your question right and that I gave you a good answer. And sorry for the late reply.
Eslam Afifi
-
You're welcome.
Eslam Afifi