about performance

T

Tony

Hello!

Here I have two versions that are almost identical. the only thing that is
different is the following rows.
This row exist in the first version
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
and this is in the second version
Console.WriteLine("{0}: {1}", rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));

As you can see both these columns CustomerID and CompanyName are of type
string. They are from the Northwind database. In the first version I write
out these by using the ordinal lookup and in the second I use strongly type
getters GetString.
I mean that if the columns have been value type like int and we have to do
boxing there would have been a difference but now when we have string which
are reference type there would not be any difference.

The reason I ask is that I read in a book named "Programming Microsoft
ado.net core reference by David Sceppa"
Here it says "The SqlDataReader class exposes Get metods for many basic .NET
data types - GetString, GetInt32,GetDateTime, and so on. These methods are
generally called the strongly typed getters. You can avoid the performance
penalty incurred by boxing and unboxing by calling the appropriate Get
method on the SqlDataReader. For example the CustomerID and CompanyName
columns contain string data. So we can use the GetString method of the
SqlDataReader to return the contens of those columns as a string, as shown
im the following code. Although the outpot appear the same, this code runs
faster than the previous code snippet.


Version 1
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string strSQL = "select CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader rdr = cmd.ExecuteReader();
int intCustomerIDOrdinal, intCompanyNameOrdinal;

intCustomerIDOrdinal = rdr.GetOrdinal("CustomerID");
intCompanyNameOrdinal = rdr.GetOrdinal("CompanyName");

while (rdr.Read())
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
rdr.Close();


Version 2
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string strSQL = "select CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader rdr = cmd.ExecuteReader();
int intCustomerIDOrdinal, intCompanyNameOrdinal;

intCustomerIDOrdinal = rdr.GetOrdinal("CustomerID");
intCompanyNameOrdinal = rdr.GetOrdinal("CompanyName");

while (rdr.Read())
Console.WriteLine("{0}: {1}",
rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));
rdr.Close();

//Tony
 
A

Arne Vajhøj

Here I have two versions that are almost identical. the only thing that
is different is the following rows.
This row exist in the first version
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
and this is in the second version
Console.WriteLine("{0}: {1}", rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));

As you can see both these columns CustomerID and CompanyName are of type
string. They are from the Northwind database. In the first version I
write out these by using the ordinal lookup and in the second I use
strongly type getters GetString.
I mean that if the columns have been value type like int and we have to
do boxing there would have been a difference but now when we have string
which are reference type there would not be any difference.

The reason I ask is that I read in a book named "Programming Microsoft
ado.net core reference by David Sceppa"
Here it says "The SqlDataReader class exposes Get metods for many basic
.NET data types - GetString, GetInt32,GetDateTime, and so on. These
methods are generally called the strongly typed getters. You can avoid
the performance penalty incurred by boxing and unboxing by calling the
appropriate Get method on the SqlDataReader. For example the CustomerID
and CompanyName columns contain string data. So we can use the GetString
method of the SqlDataReader to return the contens of those columns as a
string, as shown im the following code. Although the outpot appear the
same, this code runs faster than the previous code snippet.

The boxing/unboxing does not apply to string.

I don't really see why there should be any performance
difference between the two.

(a quick glance at the code seems to indicate though that
the code paths are somewhat different)

And if there is a difference then it should be insignificant
compared to what it takes to actually get the data up from
the database and over to the app.

Arne
 
A

Arne Vajhøj

[...]
I mean that if the columns have been value type like int and we have to
do boxing there would have been a difference but now when we have string
which are reference type there would not be any difference.

The reason I ask is that I read in a book named "Programming Microsoft
ado.net core reference by David Sceppa"
Here it says "The SqlDataReader class exposes Get metods for many basic
.NET data types - GetString, GetInt32,GetDateTime, and so on. These
methods are generally called the strongly typed getters. You can avoid
the performance penalty incurred by boxing and unboxing by calling the
appropriate Get method on the SqlDataReader. For example the CustomerID
and CompanyName columns contain string data. So we can use the GetString
method of the SqlDataReader to return the contens of those columns as a
string, as shown im the following code. Although the outpot appear the
same, this code runs faster than the previous code snippet.

IMHO, the compelling reason to use strongly-typed getters is for the
type-safety and convenience, not performance reasons.

In the context of Console.WriteLine(), type-safety doesn't really come
into play, because the WriteLine() method will do the appropriate
conversions as necessary. But there are lots of other examples where
you'd otherwise have to cast a result, where using a strong-typed getter
you can just do an assignment.

But the GetString actually does a cast, so (string)rdr[ix] and
rdr.GetString(ix) throws the same exception under the same
circumstances.

So it is not really more type safe.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top