|
Christian Graus wrote: Wow - I did not know that.
Yeah I just tested it. The claim is BS. It creates the same construct regardless of the number of cases. It will reorganize cases to make the lookup more efficient, but there is NO hashtable!
|
|
|
|
|
leppie wrote: It creates the same construct regardless of the number of cases.
Not at all.
Here is the generated code for a switch containing five case labels and one containing six case labels. Do you see the fundamental difference?
00000084 mov esi,dword ptr [ebp-44h]
00000087 test esi,esi
00000089 je 000000ED
0000008b mov edx,dword ptr ds:[02C33098h]
00000091 mov ecx,esi
00000093 call 7795C574
00000098 mov edi,eax
0000009a test edi,edi
0000009c jne 000000ED
0000009e mov edx,dword ptr ds:[02C3309Ch]
000000a4 mov ecx,esi
000000a6 call 7795C574
000000ab mov edi,eax
000000ad test edi,edi
000000af jne 000000ED
000000b1 mov edx,dword ptr ds:[02C330A0h]
000000b7 mov ecx,esi
000000b9 call 7795C574
000000be mov edi,eax
000000c0 test edi,edi
000000c2 jne 000000ED
000000c4 mov edx,dword ptr ds:[02C330A4h]
000000ca mov ecx,esi
000000cc call 7795C574
000000d1 mov edi,eax
000000d3 test edi,edi
000000d5 jne 000000ED
000000d7 mov edx,dword ptr ds:[02C330A8h]
000000dd mov ecx,esi
000000df call 7795C574
000000e4 mov edi,eax
000000e6 test edi,edi
000000e8 jne 000000ED
000000ea nop
000000eb jmp 000000ED
0000008a mov ebx,dword ptr [ebp-44h]
0000008d test ebx,ebx
0000008f je 00000148
00000095 cmp dword ptr ds:[02B284BCh],0
0000009c jne 0000011F
000000a2 mov ecx,7913386Ch
000000a7 call FCAC0A5C
000000ac mov esi,eax
000000ae mov ecx,esi
000000b0 mov edx,6
000000b5 call 75929474
000000ba push 0
000000bc mov edx,dword ptr ds:[02B23098h]
000000c2 mov ecx,esi
000000c4 call 75954260
000000c9 push 1
000000cb mov edx,dword ptr ds:[02B2309Ch]
000000d1 mov ecx,esi
000000d3 call 75954260
000000d8 push 2
000000da mov edx,dword ptr ds:[02B230A0h]
000000e0 mov ecx,esi
000000e2 call 75954260
000000e7 push 3
000000e9 mov edx,dword ptr ds:[02B230A4h]
000000ef mov ecx,esi
000000f1 call 75954260
000000f6 push 4
000000f8 mov edx,dword ptr ds:[02B230A8h]
000000fe mov ecx,esi
00000100 call 75954260
00000105 push 5
00000107 mov edx,dword ptr ds:[02B230ACh]
0000010d mov ecx,esi
0000010f call 75954260
00000114 lea edx,ds:[02B284BCh]
0000011a call 76234066
0000011f lea eax,[ebp-54h]
00000122 push eax
00000123 mov ecx,dword ptr ds:[02B284BCh]
00000129 mov edx,ebx
0000012b call 75789F50
00000130 mov esi,eax
00000132 test esi,esi
00000134 je 00000148
00000136 mov eax,dword ptr [ebp-54h]
00000139 cmp eax,6
0000013c jae 00000145
0000013e jmp dword ptr [eax*4+03C41798h]
00000145 nop
00000146 jmp 00000148
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Eish, we are really splitting micro-bits here!
What are you case labels for either case?
IMO, this JIT is way too low level to care about. Maybe doing the native layout, etc for more than 5 cases are a bit expensive.
I was looking at MSIL level though, so I didnt really know (and still dont care) about what happens under those covers (but good to know none the less)
Thanks
leppie
|
|
|
|
|
leppie wrote: Eish, we are really splitting micro-bits here!
Yes, but it can at least be good to know that if you have a lot of case labels, you don't have to try to order them for performance.
leppie wrote: What are you case labels for either case?
Just labels with strings and no code, so that it tests the performance of the switch only:
switch (selection) {
case "00":
case "01":
case "02":
case "03":
case "04":
case "05":
default: break;
}
(I tested it with code in the cases also, so that I know that it behaves in the same manner.)
My results for a 10000000 loop for each case label, with switches having five and six case labels:
00 : 105 ms.
01 : 205 ms.
02 : 303 ms.
03 : 421 ms.
04 : 500 ms.
00 : 602 ms.
01 : 605 ms.
02 : 589 ms.
03 : 622 ms.
04 : 615 ms.
05 : 577 ms.
There is definitely a measurable performance difference for the first labels in the shorter switch, but even 60 ns. isn't much to whine about.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
so we are talking 50 nanoseconds here per iteration.
|
|
|
|
|
Christian Graus wrote: scody wrote:
but in C#, order of the 'cases' doesn’t matter.
What on earth makes you think that ?
What on earth makes you think it does matter?
Christian Graus wrote: the MSIL for a switch is no different to a bunch of if/then/else statements
The MSIL for a switch is jumptable, not if/else branching. For integral types, the target offset is simply computed (not sure what happens internally), in your typical state machine fashion. For strings the behaviour could be different.
|
|
|
|
|
leppie wrote: The MSIL for a switch is jumptable, not if/else branching.
If you have five or less case labels, the code generated uses if/else logic. You can easily determine this by testing the performance, and see the difference between selecting different values.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Or you could try a Dictionary of delegates. It may at least help with maintenance issues.
|
|
|
|
|
Hi,
i have a Java application that stores a timestamp object as a bigint in a MySql database.
Now, my C# application has to convert it to a DateTime object. How do i do this. I tried using the Datetime.FromFileTime(timestamp); but the output it gives is not correct. Here is the sample.
long tm = 1217404627332;
DateTime dt = DateTime.FromFileTime(tm);
Response.Write("Time: " + dt.ToLongDateString());
The output it gives is Tuesday, January 02, 1601 when it should be Wednesday, July 31, 2008
How do i do this? Any help is appreciated. Thanks in advance.
|
|
|
|
|
One approach would be to search for how the number translates to a date in Java, and write code to do a conversion.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
The Windows FILETIME datatype, and hence .NET's DateTime, counts in 100ns intervals: there are 10 million of them in a single second. The base date is 1 January 1601. Your number, big though it is, only means 121,740 seconds or a little less than 34 hours.
You need to find out how large a time interval one tick represents in your source data, and you need to find out what the base date is, so that you can manipulate it correctly for .NET.
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
Java time represents milliseconds since January 1, 1970, so if you account for that date instead of the January 1601 date, you should be OK.
long tm = 1217404627332;
DateTime dt = new DateTime(1970,1,1,0,0,0);
dt = dt.AddMilliseconds(tm);
MessageBox.Show("Time: " + dt.ToLongDateString());
HTH - although I get Wednesday July 30,2008....closer!
-curtisk
|
|
|
|
|
There are three problems i am facing
1. If i specify (1,1) then i should get A1, how can i achieve that.
2. How can i get reference of a particular cell,without using Range class , i want to get cell's forecolor and backcolor directly.
|
|
|
|
|
i have created a winform.but i can't connect it with my database.there are no error has shown but there have 3 warning about path.but i can't find the right path.How can I solve this problem?
|
|
|
|
|
Well, hard to say without knowing what sort of DB it is, and what your warnings are, and what your connection string is.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
Can you Explain Brielfy what you have done and let us see your code that connects to the Database
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
private static string ToDbfFieldTypeString(Type pType)
{
string sResult = "";
try
{
if (pType == typeof(int))
{
sResult = "int";
return sResult;
}
if (pType == typeof(long))
{
sResult = "long";
return sResult;
}
if (pType == typeof(float))
{
sResult = "float";
return sResult;
}
if (pType == typeof(double))
{
sResult = "double";
return sResult;
}
if (pType == typeof(decimal))
{
sResult = "number";
return sResult;
}
if (pType == typeof(bool))
{
sResult = "char(2)";
return sResult;
}
if (pType == typeof(DateTime))
{
sResult = "date";
return sResult;
}
if (pType == typeof(string))
{
sResult = "string";
return sResult;
}
}
catch //(Exception excp)
{
sResult = "";
}
return sResult;
}
-------------------------------------------------------------------
i change the if-else structure to switch structure as follow:
but it tell me to input an integer value at switch();
switch (pType)
{
case typeof(int):
return "int";
case typeof(long):
return "long";
case typeof(float):
return "float";
case typeof(double):
return "double";
case typeof(decimal):
return "number";
case typeof(bool):
return "char(2)";
case typeof(DateTime):
return "date";
case typeof(string):
return "string";
}
So,buddy,could you help me modify the code to compile success?
And tell me what type of value does the switch(?) allow to use? Thanks!
|
|
|
|
|
panyl wrote: but it tell me to input an integer value at switch();
No, it tells you you need an integral type. I googled your error message and found this:
The C# compiler informs us that a switch statement can only take certain integral types. To be more specific, it can take an sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type. C# realizes that the switch has received a yyy object and since it cannot convert a yyy into the above integral types, it reports an error.
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
A switch statement can take an enum because:
"Every enumeration type has an underlying type, which can be any integral type except char." source MSDN[^]
In your situation this helps because you can use the GetTypeCode() method which returns an enum member.
deciaml d = 0;
switch (d.GetTypeCode())
{
case TypeCode.Decimal:
Console.WriteLine("It's a deciaml");
break;
case TypeCode.Int32:
Console.WriteLine("It's an int");
break;
default:
Console.WriteLine("WTF?");
break;
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
|
Hi Friends,
I have MCTS Exam(70-536 & 70-528) Document and Dumps .
I like upload the file to code project.How to upload the file to codeproject
|
|
|
|
|
That's not what CodeProject is for.
Life, family, faith: Give me a visit.
From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound."
Judah Himango
|
|
|
|
|
You need to submit a article first, and then you will see how to add a file to codeproject.
|
|
|
|
|
I have created a project which contains a main window form that spawns a series of new windows forms which are Top most application. Upon creation of each new window the focus stays with the current application( Firefox for example) as desired.
The problem occurs after I close one of the spawned windows using (this.close). The spawned form correctly returns focus to the users application which had focus last. However when the next spawn appears, the focus is taken away and given to this program.
Any Ideas?
|
|
|
|
|
Zocrates wrote: Any Ideas?
Are you still operating forms in multiple threads. My example using timers to create and show a second form still never steals the focus from another application like Notepad. I just keep typing into Notepad while my second form appears on top then, on a timer, closes, then appears again, and closes again.
led mike
|
|
|
|
|