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 = strSingle;
else
strChar = strDouble;
}
dTicksStart = Environment.TickCount;
int iOk = 0, iXX = 0;
for(int i = 0; i<iNumStrings; i++)
{
if( ValueWithinRange( strChar ) )
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 );
}
}
}
}
}