Re: How to check whether a string can be casted to be Long ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: PeterB (peter_at_data.se)
Date: 10/22/04


Date: Fri, 22 Oct 2004 10:00:19 +0200

Hmm, perhaps this is different on a PC than on a PDA, but all my tests show
that exceptions slow down quite a lot, regardless if you are running under
debugger or optimized release version. My test was done on a PC and it's a
release version of the test application.

Worth noting is that when increasing the number of lines (10times the
previous test) tested in a testrun, the time to run the test without
exceptions increased by a factor 2, while the test with exceptions increased
by a factor 10.

Below is the code and the testresults.

The results (strings = number of strings casted, OK = no exception, Exc =
Exception was thrown, Ticks=total amount of ticks for the testrun):
Strings: 1000, OK = 1000, Exc = 0 Ticks: 0
Strings: 1000, OK = 1000, Exc = 0 Ticks: 0
Strings: 1000, OK = 0, Exc = 1000 Ticks: 63
Strings: 1000, OK = 0, Exc = 1000 Ticks: 47
Strings: 1000, OK = 0, Exc = 1000 Ticks: 47
Strings: 10000, OK = 10000, Exc = 0 Ticks: 0
Strings: 10000, OK = 10000, Exc = 0 Ticks: 16
Strings: 10000, OK = 10000, Exc = 0 Ticks: 16
Strings: 10000, OK = 10000, Exc = 0 Ticks: 15
Strings: 10000, OK = 0, Exc = 10000 Ticks: 422
Strings: 10000, OK = 0, Exc = 10000 Ticks: 438
Strings: 10000, OK = 0, Exc = 10000 Ticks: 422
Strings: 10000, OK = 0, Exc = 10000 Ticks: 407
Strings: 100000, OK = 100000, Exc = 0 Ticks: 31
Strings: 100000, OK = 100000, Exc = 0 Ticks: 31
Strings: 100000, OK = 100000, Exc = 0 Ticks: 32
Strings: 100000, OK = 100000, Exc = 0 Ticks: 32
Strings: 100000, OK = 0, Exc = 100000 Ticks: 4141
Strings: 100000, OK = 0, Exc = 100000 Ticks: 4141
Strings: 100000, OK = 0, Exc = 100000 Ticks: 4203
Strings: 100000, OK = 3, Exc = 99997 Ticks: 4172

The code (this is for PC appliaction) not CF!!:
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO;

using System.Text;

namespace WinTestOvrFlwExcSpeed

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.CheckBox checkBox1;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.TextBox txtOutputFileName;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.textBox2 = new System.Windows.Forms.TextBox();

this.checkBox1 = new System.Windows.Forms.CheckBox();

this.button1 = new System.Windows.Forms.Button();

this.textBox1 = new System.Windows.Forms.TextBox();

this.txtOutputFileName = new System.Windows.Forms.TextBox();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// textBox2

//

this.textBox2.Location = new System.Drawing.Point(16, 8);

this.textBox2.MaxLength = 3276700;

this.textBox2.Multiline = true;

this.textBox2.Name = "textBox2";

this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;

this.textBox2.Size = new System.Drawing.Size(384, 304);

this.textBox2.TabIndex = 3;

this.textBox2.Text = "";

//

// checkBox1

//

this.checkBox1.Location = new System.Drawing.Point(296, 320);

this.checkBox1.Name = "checkBox1";

this.checkBox1.TabIndex = 4;

this.checkBox1.Text = "With exceptions";

//

// button1

//

this.button1.Location = new System.Drawing.Point(296, 352);

this.button1.Name = "button1";

this.button1.TabIndex = 5;

this.button1.Text = "Test";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(120, 344);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(88, 20);

this.textBox1.TabIndex = 6;

this.textBox1.Text = "1000";

//

// txtOutputFileName

//

this.txtOutputFileName.Location = new System.Drawing.Point(120, 320);

this.txtOutputFileName.Name = "txtOutputFileName";

this.txtOutputFileName.Size = new System.Drawing.Size(168, 20);

this.txtOutputFileName.TabIndex = 7;

this.txtOutputFileName.Text = "output.txt";

//

// label1

//

this.label1.Location = new System.Drawing.Point(20, 320);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(96, 20);

this.label1.TabIndex = 8;

this.label1.Text = "Save output to:";

this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// label2

//

this.label2.Location = new System.Drawing.Point(16, 344);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(100, 20);

this.label2.TabIndex = 9;

this.label2.Text = "Repeat:";

this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(416, 382);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.txtOutputFileName);

this.Controls.Add(this.textBox1);

this.Controls.Add(this.textBox2);

this.Controls.Add(this.checkBox1);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private bool ValueWithinRange( string strInt )

{

try

{

int iTmp = Convert.ToInt32(strInt);

if( iTmp > int.MaxValue || iTmp < int.MinValue )

return false;

else

return true;

}

catch( OverflowException )

{

return false;

}

}

private void button1_Click(object sender, System.EventArgs e)

{

double dTicksStart, dTicksEnd;

int iNumStrings = Convert.ToInt32(this.textBox1.Text);

string[] strChar = new string[iNumStrings];

System.Random rnd = new Random();

for( int i = 0; i<iNumStrings; i++)

{

int r = rnd.Next();

string strSingle = Convert.ToString(r);

string strDouble = Convert.ToString(r) +
Convert.ToString(r);///50000000000);

if( !checkBox1.Checked )

strChar[i] = strSingle;

else

strChar[i] = strDouble;

}

dTicksStart = Environment.TickCount;

int iOk = 0, iXX = 0;

for(int i = 0; i<iNumStrings; i++)

{

if( ValueWithinRange( strChar[i] ) )

iOk++;

else

iXX++;

}

dTicksEnd = Environment.TickCount;

double res = dTicksEnd - dTicksStart;

textBox2.Text += "Strings: " + iNumStrings.ToString() +

", OK = " + iOk.ToString() +

", Exc = " + iXX.ToString() +

" Ticks: " + res.ToString() + "\r\n";

string file = this.txtOutputFileName.Text;

StreamWriter sw;

using( sw = new StreamWriter( file, false, Encoding.Unicode ) )

{

foreach( string line in textBox2.Lines )

{

sw.WriteLine( line );

}

}

}

}

}

"Alex Feinman [MVP]" <public_news@alexfeinman.com> skrev i meddelandet
news:elz9i88tEHA.1276@TK2MSFTNGP12.phx.gbl...
> No, it does not. It only slows down when run under debugger
>
> --
> Alex Feinman
> ---
> Visit http://www.opennetcf.org
> "Kempton" <Kempton@discussions.microsoft.com> wrote in message
> news:2326E531-FC0F-438D-B0EE-F86F053B0B93@microsoft.com...
>> Thanks for reply.
>>
>> I already do that but my problem is it makes the software run slower or
>> even
>> paused for a little while when exception occur while my application has
>> to
>> process 10 data per second real time. So I really wants to check the
>> string
>> before I cast it in order to prevent the invalidcastexception.
>>
>> Kempton
>>
>> "Ginny Caughey [MVP]" wrote:
>>
>>> Kempton,
>>>
>>> You can include the code within a try...catch block.
>>>
>>> --
>>> Ginny Caughey
>>> ..Net Compact Framework MVP
>>>
>>>
>>>
>>> "Kempton" <Kempton@discussions.microsoft.com> wrote in message
>>> news:1A79604B-6EC9-46E2-A1C0-8A2356DE99F4@microsoft.com...
>>> >I need to read data from serialport and then cast them to Long by using
>>> >vb
>>> > .net on Ce device. Sometimes I will have InvalidCastException. How
>>> > can I
>>> > make sure the string can be cast long before I do it in order to
>>> > prevent
>>> > the
>>> > InvalidCastException?
>>>
>>>
>>>
>
>



Relevant Pages

  • Re: Question concerning object-oriented programming
    ... If at the end of the constructor the ... return defaults such as zero, a null string, or null. ... can also throw exceptions from methods. ... toString() in there. ...
    (comp.programming)
  • Re: Control = StrConv([Control], 3)
    ... Scott ... >> Fred- I think a list of exceptions would be best, I can only see a handful ... > Function ConvExceptionsInField(StrIn As String) As String ... > Dim strWord As String ...
    (microsoft.public.access.formscoding)
  • Re: Java 7 features
    ... indeed, get rid of checked exceptions, by making ... String foo = whatever; ... The example above explicitly initializes the String foo. ...
    (comp.lang.java.programmer)
  • Cant understand why these exceptions occur...
    ... If the command is "buffer", ... but I get exceptions if the buffer file does ... procedure LoadCache(Const cacheName: String); forward; ... if (idx < Length(files)) then ...
    (alt.comp.lang.borland-delphi)
  • Re: Neue Attribute einem Objekt =?ISO-8859-1?Q?hinzuf=FCgen?=
    ... Mit dem statsichen Typsystem hat das also nichts zu tun, sondern nur mit der Frage, ob Exceptions checked oder unchecked sein sollen. ... Für mich checked Exceptions zumindest mal was statisches und Fallen damit in die gleiche Kategorie wie das Typsystem. ... An diversen Stellen hat man code der Art "info".equalsstehen, wobei str der besagte String ist und natürlich nicht an jeder Stelle "info" als Vergleichswert herangezogen wird. ... Denn selbst wenn ich den Typ von str ändere, habe ich noch immer einen falschen Methodenaufruf drin. ...
    (de.comp.lang.java)