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 10 1582
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" <ti*******@hotmail.com> wrote in message
news:f0**************************@posting.google.c om... 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
quick refactor:
e.Handled = !(isValidNumber && >= byte.MinValue && d <= byte.MaxValue);
;)
Regards,
Matt
"Matt Garven" <ma**@nooospam.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl... 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" <ti*******@hotmail.com> wrote in message news:f0**************************@posting.google.c om... 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
ti*******@hotmail.com (Timothy Graves) wrote in
news:f0**************************@posting.google.c om: 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...esktopdevelope
r/default.aspx
or TinyURL: http://tinyurl.com/ogmm
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
Timothy Graves <ti*******@hotmail.com> wrote: 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.)
<snip>
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?
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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 <sk***@pobox.com> wrote in message news:<MP************************@news.microsoft.co m>... Timothy Graves <ti*******@hotmail.com> wrote: 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.)
<snip>
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?
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 <sk***@pobox.com> wrote in message news:<MP************************@news.microsoft.co m>... Timothy Graves <ti*******@hotmail.com> wrote: 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.)
<snip>
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?
I think you are right. I was getting a little too ambitious.
Thanks Chris,
Tim
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message news:<Xn*********************************@207.46.2 48.16>... ti*******@hotmail.com (Timothy Graves) wrote in news:f0**************************@posting.google.c om:
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...esktopdevelope r/default.aspx
or TinyURL:
http://tinyurl.com/ogmm
Hope this helps.
Chris. ------------- C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
I think you are right. I was getting a little too ambitious.
Thanks Chris,
Tim
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message news:<Xn*********************************@207.46.2 48.16>... ti*******@hotmail.com (Timothy Graves) wrote in news:f0**************************@posting.google.c om:
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...esktopdevelope r/default.aspx
or TinyURL:
http://tinyurl.com/ogmm
Hope this helps.
Chris. ------------- C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/ ti*******@hotmail.com (Timothy Graves) wrote in message news:<f0**************************@posting.google. com>... 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. kt*******@sneakemail.com (Matt) wrote in message news:<55*************************@posting.google.c om>... ti*******@hotmail.com (Timothy Graves) wrote in message news:<f0**************************@posting.google. com>... 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.
That did it, the exception runs almost at zero now.
Thanks for figuring that out for me
Tim This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Snyke |
last post by:
Hi.
I have a command line script which works really fine, the only problem
is that it take *really* long for the first output to be printed on
screen.
Since I also get some HTTP headers I'm...
|
by: OvErboRed |
last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding
checked exceptions (the longest-running of which seems to be <cJQQ9.4419
$j94.834878@news02.tsnz.net>.
My personal...
|
by: dvumani |
last post by:
I have C code which computes the row sums of a matrix, divide each
element of each row with the row sum and then compute the column sum of
the resulting matrix. Is there a way I can speed up the...
|
by: Robert Wilkens |
last post by:
Ok... This may be the wrong forum, but it's the first place I'm trying.
I'm new to C# and just implemented the 3-tier Distributed application from
Chapter 1 (the first walkthrough) in the...
|
by: RepStat |
last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
|
by: Anonieko |
last post by:
Understanding and Using Exceptions
(this is a really long post...only read it if you (a) don't know what
try/catch is OR (b) actually write catch(Exception ex) or catch{ })
The first thing I...
|
by: RobinAG |
last post by:
Hello,
I just split my database into front and back end. My front end users are experiencing really slow opening of forms. I've searched online for help speeding up forms, but I'm not sure what...
|
by: ags5406 |
last post by:
I've created an application that downloads data daily from a secure
web site and stores that data in an Access database. Then there are
different options for allowing the user to do keyword and...
|
by: RedSon |
last post by:
Chapter 3: What are the most common Exceptions and what do they mean?
As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |