Speeding up exceptions or my bad code

T

Timothy Graves

I have a quick (pun intended) question for the guru's out there.

I have a piece of code where I am validating the input of chancters
into a cell in a datagrid. I am using the keypressed event to get the
charcter that the user typed and then allowing it to be passed to the
cell. Now here is the tricked part, I am also validating the values
(max and min) so that the user does not input a invalid number (out of
range ex. byte != 256).

Here is my code.

//////START CODE//////

DateTime begin = DateTime.Now;
try
{
System.Convert.ToByte(tableDataGrid.Controls[tableDataGrid.CurrentCell.ColumnNumber
+ 2].Text + e.KeyChar);

//This takes the current value in the cell (before editing has
finished)
//and appends the current pressed key and tries to convert it to a
byte

e.Handled = false;
//if no exception is throwed then it is allowed to be sent to the
cell
}
catch(System.OverflowException)
{
e.Handled = true;
//if an exception does occur then it is not passed
}
DateTime end = DateTime.Now;

info.Text = Convert.ToString(end - begin);
//just a multi line text box that I used for debugging and performance

//////END CODE//////

The reason that I am asking this is that exceptions are costly
performance wise. This code in the block takes 2.2656250 seconds to
be performed when an exception is thrown. But without one the ode
executes too quickly to be timed (sp?).

Do ya'll see any way to increase the speed of the exception or code.
I'm up for anythng really.

Thanks all

Tim
 
M

Matt Garven

Avoid the exception altogether.

string s = "255";
double d = 0;
byte b = 0;

bool isValidNumber = Double.TryParse(s,
System.Globalization.NumberStyles.Integer, null, out d);
e.Handled = !(isValidNumber && d >= 0 && d <= 255);

Regards,
Matt
 
C

Chris R. Timmons

(e-mail address removed) (Timothy Graves) wrote in
I have a quick (pun intended) question for the guru's out there.

I have a piece of code where I am validating the input of
chancters into a cell in a datagrid. I am using the keypressed
event to get the charcter that the user typed and then allowing
it to be passed to the cell. Now here is the tricked part, I am
also validating the values (max and min) so that the user does
not input a invalid number (out of range ex. byte != 256).

Here is my code.

//////START CODE//////

DateTime begin = DateTime.Now;
try
{
System.Convert.ToByte(tableDataGrid.Controls[tableDataGrid.Cu
rrentCell.ColumnNumber
+ 2].Text + e.KeyChar);

//This takes the current value in the cell (before editing
has
finished)
//and appends the current pressed key and tries to convert
it to a
byte

e.Handled = false;
//if no exception is throwed then it is allowed to be sent
to the
cell
}
catch(System.OverflowException)
{
e.Handled = true;
//if an exception does occur then it is not passed
}
DateTime end = DateTime.Now;

info.Text = Convert.ToString(end - begin);
//just a multi line text box that I used for debugging and
performance

//////END CODE//////

The reason that I am asking this is that exceptions are costly
performance wise. This code in the block takes 2.2656250
seconds to be performed when an exception is thrown. But
without one the ode executes too quickly to be timed (sp?).

Do ya'll see any way to increase the speed of the exception or
code. I'm up for anythng really.

Tim,

To solve the speed problem, don't use exceptions at all. Simply
filter each character and discard those that don't meet your
criteria:

// Character-level validation.
// Allow only 0 thru 9.
e.Handled = ((e.KeyChar < '0') || (e.KeyChar > '9'));

I would suggest not testing the entire value after each keystroke.
Instead, test it after the user tries to leave the field or submit
the form (i.e. use field-level validation instead of character-level
validation). The logic is much simpler, plus I've found that users
are more comfortable with this approach, rather than getting an error
message after each keystroke.

Here's an article w/ code that makes field-level validation in
WinForms much easier:

http://www.ftponline.com/vsm/2002_11/magazine/columns/desktopdevelope
r/default.aspx

or TinyURL:

http://tinyurl.com/ogmm


Hope this helps.

Chris.
 
M

Matt Garven

quick refactor:

e.Handled = !(isValidNumber && >= byte.MinValue && d <= byte.MaxValue);

;)

Regards,
Matt


Matt Garven said:
Avoid the exception altogether.

string s = "255";
double d = 0;
byte b = 0;

bool isValidNumber = Double.TryParse(s,
System.Globalization.NumberStyles.Integer, null, out d);
e.Handled = !(isValidNumber && d >= 0 && d <= 255);

Regards,
Matt

Timothy Graves said:
I have a quick (pun intended) question for the guru's out there.

I have a piece of code where I am validating the input of chancters
into a cell in a datagrid. I am using the keypressed event to get the
charcter that the user typed and then allowing it to be passed to the
cell. Now here is the tricked part, I am also validating the values
(max and min) so that the user does not input a invalid number (out of
range ex. byte != 256).

Here is my code.

//////START CODE//////

DateTime begin = DateTime.Now;
try
{
System.Convert.ToByte(tableDataGrid.Controls[tableDataGrid.CurrentCell.Colum
nNumber
+ 2].Text + e.KeyChar);

//This takes the current value in the cell (before editing has
finished)
//and appends the current pressed key and tries to convert it to a
byte

e.Handled = false;
//if no exception is throwed then it is allowed to be sent to the
cell
}
catch(System.OverflowException)
{
e.Handled = true;
//if an exception does occur then it is not passed
}
DateTime end = DateTime.Now;

info.Text = Convert.ToString(end - begin);
//just a multi line text box that I used for debugging and performance

//////END CODE//////

The reason that I am asking this is that exceptions are costly
performance wise. This code in the block takes 2.2656250 seconds to
be performed when an exception is thrown. But without one the ode
executes too quickly to be timed (sp?).

Do ya'll see any way to increase the speed of the exception or code.
I'm up for anythng really.

Thanks all

Tim
 
J

Jon Skeet

Timothy Graves said:
I have a quick (pun intended) question for the guru's out there.

I have a piece of code where I am validating the input of chancters
into a cell in a datagrid. I am using the keypressed event to get the
charcter that the user typed and then allowing it to be passed to the
cell. Now here is the tricked part, I am also validating the values
(max and min) so that the user does not input a invalid number (out of
range ex. byte != 256).

<snip>

As well as avoiding the exception by using other validation code, it's
worth knowing why the exception is taking so long. (You should also be
catching FormatException, by the way.)

The reason that I am asking this is that exceptions are costly
performance wise. This code in the block takes 2.2656250 seconds to
be performed when an exception is thrown. But without one the ode
executes too quickly to be timed (sp?).

2 seconds is a very long time for an exception to take. What I suspect
is happening is that the first time *any* exception is thrown, other
assemblies are loaded to support that. Unfortunately I can't reproduce
the problem - I ran this program:

using System;

class Test
{
public static void Main(string[] args)
{
for (int i=0; i < 5; i++)
{
DateTime start = DateTime.Now;
try
{
Convert.ToByte("-10");
}
catch (OverflowException)
{
}
DateTime end = DateTime.Now;
Console.WriteLine (end-start);
}
}
}

and the first time it ran, the first exception took 1/10th of a second,
all the rest being almost instantaneous. After that, all runs gave
almost instantaneous results.

What does the above do on your computer?
 
T

Timothy Graves

I ran your program compile with .net framework 1.1 (VS 2003) and here
was my results:

00:00:02.3125000
00:00:00
00:00:00
00:00:00
00:00:00

When I ran it under 1.0 (VS 2002)

00:00:00.1250000
00:00:00
00:00:00.0156250
00:00:00
00:00:00

I have no idea what could have caused this. I also tried it in
sharpdevelop (with 1.1) and it was just like VS 2002.

Well any way that got me stumped.

Jon Skeet said:
Timothy Graves said:
I have a quick (pun intended) question for the guru's out there.

I have a piece of code where I am validating the input of chancters
into a cell in a datagrid. I am using the keypressed event to get the
charcter that the user typed and then allowing it to be passed to the
cell. Now here is the tricked part, I am also validating the values
(max and min) so that the user does not input a invalid number (out of
range ex. byte != 256).

<snip>

As well as avoiding the exception by using other validation code, it's
worth knowing why the exception is taking so long. (You should also be
catching FormatException, by the way.)

The reason that I am asking this is that exceptions are costly
performance wise. This code in the block takes 2.2656250 seconds to
be performed when an exception is thrown. But without one the ode
executes too quickly to be timed (sp?).

2 seconds is a very long time for an exception to take. What I suspect
is happening is that the first time *any* exception is thrown, other
assemblies are loaded to support that. Unfortunately I can't reproduce
the problem - I ran this program:

using System;

class Test
{
public static void Main(string[] args)
{
for (int i=0; i < 5; i++)
{
DateTime start = DateTime.Now;
try
{
Convert.ToByte("-10");
}
catch (OverflowException)
{
}
DateTime end = DateTime.Now;
Console.WriteLine (end-start);
}
}
}

and the first time it ran, the first exception took 1/10th of a second,
all the rest being almost instantaneous. After that, all runs gave
almost instantaneous results.

What does the above do on your computer?
 
T

Timothy Graves

I think you are right. I was getting a little too ambitious.

Thanks Chris,

Tim

Chris R. Timmons said:
(e-mail address removed) (Timothy Graves) wrote in
I have a quick (pun intended) question for the guru's out there.

I have a piece of code where I am validating the input of
chancters into a cell in a datagrid. I am using the keypressed
event to get the charcter that the user typed and then allowing
it to be passed to the cell. Now here is the tricked part, I am
also validating the values (max and min) so that the user does
not input a invalid number (out of range ex. byte != 256).

Here is my code.

//////START CODE//////

DateTime begin = DateTime.Now;
try
{
System.Convert.ToByte(tableDataGrid.Controls[tableDataGrid.Cu
rrentCell.ColumnNumber
+ 2].Text + e.KeyChar);

//This takes the current value in the cell (before editing
has
finished)
//and appends the current pressed key and tries to convert
it to a
byte

e.Handled = false;
//if no exception is throwed then it is allowed to be sent
to the
cell
}
catch(System.OverflowException)
{
e.Handled = true;
//if an exception does occur then it is not passed
}
DateTime end = DateTime.Now;

info.Text = Convert.ToString(end - begin);
//just a multi line text box that I used for debugging and
performance

//////END CODE//////

The reason that I am asking this is that exceptions are costly
performance wise. This code in the block takes 2.2656250
seconds to be performed when an exception is thrown. But
without one the ode executes too quickly to be timed (sp?).

Do ya'll see any way to increase the speed of the exception or
code. I'm up for anythng really.

Tim,

To solve the speed problem, don't use exceptions at all. Simply
filter each character and discard those that don't meet your
criteria:

// Character-level validation.
// Allow only 0 thru 9.
e.Handled = ((e.KeyChar < '0') || (e.KeyChar > '9'));

I would suggest not testing the entire value after each keystroke.
Instead, test it after the user tries to leave the field or submit
the form (i.e. use field-level validation instead of character-level
validation). The logic is much simpler, plus I've found that users
are more comfortable with this approach, rather than getting an error
message after each keystroke.

Here's an article w/ code that makes field-level validation in
WinForms much easier:

http://www.ftponline.com/vsm/2002_11/magazine/columns/desktopdevelope
r/default.aspx

or TinyURL:

http://tinyurl.com/ogmm


Hope this helps.

Chris.
 
M

Matt

I ran your program compile with .net framework 1.1 (VS 2003) and here
was my results:

00:00:02.3125000
00:00:00
00:00:00
00:00:00
00:00:00

When I ran it under 1.0 (VS 2002)

00:00:00.1250000
00:00:00
00:00:00.0156250
00:00:00
00:00:00

I have no idea what could have caused this. I also tried it in
sharpdevelop (with 1.1) and it was just like VS 2002.

Well any way that got me stumped.

I take it you're running this from within Visual Studio, as opposed to
running the .exe directly? Try selecting Debug -> Start Without
Debugging from the menu and see what the timings are then.
 
T

Timothy Graves

I take it you're running this from within Visual Studio, as opposed to
running the .exe directly? Try selecting Debug -> Start Without
Debugging from the menu and see what the timings are then.

That did it, the exception runs almost at zero now.

Thanks for figuring that out for me

Tim
 

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

Similar Threads

Asp.net Important Topics. 0

Top