Click here to Skip to main content
15,916,091 members
Home / Discussions / C#
   

C#

 
GeneralRe: Photo Printing Wizard Pin
Heath Stewart25-Mar-05 12:55
protectorHeath Stewart25-Mar-05 12:55 
GeneralRe: Photo Printing Wizard Pin
tommazzo25-Mar-05 13:11
tommazzo25-Mar-05 13:11 
GeneralRe: Photo Printing Wizard Pin
Dave Kreskowiak25-Mar-05 13:42
mveDave Kreskowiak25-Mar-05 13:42 
GeneralRe: Photo Printing Wizard Pin
tommazzo25-Mar-05 13:52
tommazzo25-Mar-05 13:52 
GeneralRe: Photo Printing Wizard Pin
Heath Stewart25-Mar-05 14:21
protectorHeath Stewart25-Mar-05 14:21 
GeneralRe: Photo Printing Wizard Pin
tommazzo26-Mar-05 7:36
tommazzo26-Mar-05 7:36 
GeneralRe: Photo Printing Wizard Pin
Heath Stewart26-Mar-05 8:16
protectorHeath Stewart26-Mar-05 8:16 
Generaltypeof versus Type.GetType() Pin
Member 9625-Mar-05 5:50
Member 9625-Mar-05 5:50 
I ran a test (below) and typeof is many orders of magnitude faster than Type.GetType. I guess it's because of the searching that needs to be done.

My question is why does Microsoft show in all their examples of creating a datatable to use Type.GetType instead of typeof as the columns data type parameter?
[I.E. table.Columns.Add("name",Type.GetType("System.String")) instead of table.Columns.Add("name",typeof(System.String)) ]

I'm working on a project right now where I need to convert a hiearchical business object into a flat datatable for reporting purposes. There are a large number of columns to generate in code for many different objects so I'd prefer to go with what is fastest.

I'm fairly certain that typeof and Type.GetType are functionally identical in this case but I'm wondering why MS goes with the slower option in their samples?

Or is my testing wrong?

(Test for checking performance of typeof versus Type.GetType(""))
private void button1_Click(object sender, System.EventArgs e)<br />
		{<br />
			Cursor.Current=Cursors.WaitCursor;<br />
			<br />
			System.DateTime dtThen=DateTime.Now;<br />
			Type t;<br />
<br />
<br />
			for(int InfamousI=0;InfamousI<500000;InfamousI++)<br />
			{<br />
//Doing it this way takes .09 seconds on my computer<br />
<br />
				t=typeof(System.String);<br />
				t=typeof(System.DateTime);<br />
				t=typeof(System.Int32);<br />
				t=typeof(System.String);<br />
				t=typeof(System.DateTime);<br />
				t=typeof(System.Int32);<br />
				t=typeof(System.String);<br />
				t=typeof(System.DateTime);<br />
				t=typeof(System.Int32);<br />
				t=typeof(System.String);<br />
				t=typeof(System.DateTime);<br />
				t=typeof(System.Int32);<br />
<br />
<br />
//Doing it this way takes 8.9 seconds on my computer:<br />
<br />
//				t=Type.GetType("System.String");<br />
//				t=Type.GetType("System.DateTime");<br />
//				t=Type.GetType("System.Int32");<br />
//				t=Type.GetType("System.String");<br />
//				t=Type.GetType("System.DateTime");<br />
//				t=Type.GetType("System.Int32");<br />
//				t=Type.GetType("System.String");<br />
//				t=Type.GetType("System.DateTime");<br />
//				t=Type.GetType("System.Int32");<br />
//				t=Type.GetType("System.String");<br />
//				t=Type.GetType("System.DateTime");<br />
//				t=Type.GetType("System.Int32");<br />
//				<br />
			}<br />
			MessageBox.Show("Elapsed: " + (System.DateTime.Now-dtThen).ToString());<br />
		}

GeneralRe: typeof versus Type.GetType() Pin
Heath Stewart25-Mar-05 11:34
protectorHeath Stewart25-Mar-05 11:34 
GeneralRe: typeof versus Type.GetType() Pin
Member 9625-Mar-05 13:59
Member 9625-Mar-05 13:59 
GeneralRe: typeof versus Type.GetType() Pin
Heath Stewart25-Mar-05 14:31
protectorHeath Stewart25-Mar-05 14:31 
GeneralRe: typeof versus Type.GetType() Pin
Tom Larsen25-Mar-05 12:21
Tom Larsen25-Mar-05 12:21 
GeneralRe: typeof versus Type.GetType() Pin
Heath Stewart25-Mar-05 14:28
protectorHeath Stewart25-Mar-05 14:28 
GeneralRe: typeof versus Type.GetType() Pin
Member 9625-Mar-05 16:39
Member 9625-Mar-05 16:39 
GeneralRe: typeof versus Type.GetType() Pin
Shardool25-Mar-05 14:39
Shardool25-Mar-05 14:39 
GeneralGenerate Word Document in C# Pin
Michele@MERC25-Mar-05 5:24
Michele@MERC25-Mar-05 5:24 
GeneralRe: Generate Word Document in C# Pin
Heath Stewart25-Mar-05 12:16
protectorHeath Stewart25-Mar-05 12:16 
GeneralRe: Generate Word Document in C# Pin
Michele@MERC29-Mar-05 4:13
Michele@MERC29-Mar-05 4:13 
GeneralRe: Generate Word Document in C# Pin
Heath Stewart29-Mar-05 5:29
protectorHeath Stewart29-Mar-05 5:29 
GeneralRe: Generate Word Document in C# Pin
Michele@MERC29-Mar-05 6:29
Michele@MERC29-Mar-05 6:29 
GeneralRe: Generate Word Document in C# Pin
Heath Stewart29-Mar-05 8:35
protectorHeath Stewart29-Mar-05 8:35 
GeneralRe: Generate Word Document in C# Pin
Michele@MERC30-Mar-05 3:30
Michele@MERC30-Mar-05 3:30 
GeneralRe: Generate Word Document in C# Pin
Heath Stewart30-Mar-05 5:17
protectorHeath Stewart30-Mar-05 5:17 
Generalprinting results from printer Pin
Member 178719025-Mar-05 4:45
Member 178719025-Mar-05 4:45 
GeneralRe: printing results from printer Pin
Heath Stewart25-Mar-05 11:11
protectorHeath Stewart25-Mar-05 11:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.