473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Deferred execution versus immediate execution--what's the difference?

I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?

RL
Sep 16 '08 #1
4 3365
On Sep 16, 11:22 am, raylopez99 <raylope...@yahoo.comwrote:
I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?

RL
Consider this code:

static void Main(string[] args) {

//An array of integers
int[] numbers = { 1, 2, 3, 4};

//query the array for all even numbers
var query = from n in numbers
select Square(n);

//iterate through the results
foreach (int i in query) {
Console.WriteLine(i.ToString());
}
Console.ReadLine();
}

public static double Square(double n) {
Console.WriteLine("Computing Square(" + n + ")...");
return Math.Pow(n, 2);
}

When you run it, you see this output:

Computing Square(1)...
1
Computing Square(2)...
4
Computing Square(3)...
9
Computing Square(4)...
16

Notice that the line that says "Computing Square" for each number is
not executed until the foreach is executed. In other words the Square
method is not called until you iterate the query. But if you change
the foreach to look like this (note the addition of ToList():

//iterate through the results
foreach (int i in query.ToList()) {
Console.WriteLine(i.ToString());
}

The result is this:

Computing Square(1)...
Computing Square(2)...
Computing Square(3)...
Computing Square(4)...
1
4
9
16

All the squares are calculated before they are displayed. The first
example is deferred execution of the query. The second example
(ToList) is immediate execution.

Hope this helps a little.

Chris
Sep 16 '08 #2
raylopez99 <ra********@yahoo.comwrote:
I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?
Deferred execution means that nothing really happens until you start
iterating through the data.

Immediate execution means that the data starts flowing *immediately*.

For instance, the "Where" method just sets up a data pipeline which
will filter data appropriate *when the result is enumerated*. "Count"
however needs to count the data immediately - it can't give you a
result without fetching the data.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 16 '08 #3
On Sep 16, 12:49*pm, Chris Dunaway <dunaw...@gmail.comwrote:
>
All the squares are calculated before they are displayed. *The first
example is deferred execution of the query. *The second example
(ToList) is immediate execution.

Hope this helps a little.
Thanks, it did help.

It's odd looking but I will compile it and post later this month if I
find bugs (doing my real day job now, no time to code). Var,
IEnumerable used in this context and "select" are new to me, but I'll
figure it out. Looks like you might have a few lines out of sequence
but that's no big deal.

RL
Sep 17 '08 #4
raylopez99 <ra********@yahoo.comwrote:

Looks like you might have a few lines out of sequence but that's no big deal.
No, the code Chris provided works fine. If you think that it's out of
sequence because the query expression is "from ... select ..." instead
of the other way round (like SQL) then that's just the way LINQ is -
and for good reason. It's a much more logical ordering, showing the
data flowing through the query from beginning to end.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 18 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Naresh Agarwal | last post by:
Hi What are the different kinds of JVMs exist and in what respect do they differ. What is the difference between client, server, classic and hotspot jvms? thanks, Naresh
0
by: Luc Martineau | last post by:
Hello When we create a table, what is the difference between the REFERENCE constraint on column and the FOREIGN KEY constraint on table ? I guess that the FOREIGN KEY constraint assures us...
81
by: sinister | last post by:
I wanted to spiff up my overly spartan homepage, and started using some CSS templates I found on a couple of weblogs. It looks fine in my browser (IE 6.0), but it doesn't print right. I tested...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
1
by: Hagen Hoepfner | last post by:
Dear list, i am looking for a way to check, whether an DML-statement can be performed on a current database. That means, that i want to check, if e.g. an INSERT is possible or not (due to...
4
by: aj | last post by:
DB2 8.2 LUW FP14 Is there any real difference between select blahblahblah... where blah IN (select blah......) versus select blahblahblah... where blah = ANY (select blah.....) versus select...
1
by: =?Utf-8?B?RGFwcGVyRGFuSEBub3NwYW0ubm9zcGFt?= | last post by:
Given the example below, can someone explain why TimeSpan.TotalDays gives a different result than subtracting 2 DateTime.ToOADates? I am completely stumped. Thanks in advance, Dan Example...
17
by: Suresh Pillai | last post by:
I am performing simulations on networks (graphs). I have a question on speed of execution (assuming very ample memory for now). I simplify the details of my simulation below, as the question I...
3
by: RobG | last post by:
There has been a discussion on the iPhone web development group where an opinion has been expressed that function expressions are bad for performance and can be avoided by using function...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.