|
||
|
|
#1 (permalink) | |
|
Forum Expert
|
Alrighty, this is what I saw:
Quote:
I was wonder about two things, one is the answers to the above. The forums I found it at did answer a few, but yea. I thought of another thing... Code:
if( ((from != null) || (from.Map != Map.Internal)) || (!from.Deleted) )
{
//code
}
or
if( from != null )
{
if( from.Map != Map.Internal )
{
if( !from.Deleted )
{
//code
}
}
}
__________________
|
|
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
|
I believe that if you have "Optimize code" check in the properties that those ifs will compile to the same MSIL code.
And I thought int was the same as System.Int32 on 32 bit systems and the same as System.Int64 on 64 bit systems with no performance loss or gain by using either...
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial |
|
|
|
|
|
#3 (permalink) | |
|
Forum Expert
|
Quote:
Well they stated that the System.Int32 compared to int earned no gain in performance, but the website I found that little chat there at is very interesting. CodeGuru Forums - 6 C# fundamental questions
__________________
|
|
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
it is an alias..easier to read the code..
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
|
My understand was that int was just a language keywrod and when a compiler in a 32-bit OS sees int, it actually uses System.Int32, and a 64-bit OS actually uses System.Int64.
Oh and for that Code:
int a, b, c; Code:
int a; int b; int c; And it looks like the first two replies to that post are correct in what they say...
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial |
|
|
|
|
|
#6 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
No, it doesnt. you need to use explicitly 64 bit integers.
System.Int16 = short System.Int32 = int System.Int64 = long
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." |
|
|
|
|
|
#7 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
|
hmmm whoops yeah, i just remembered reading that somewhere. My bad
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial |
|
|
|
|
|
#8 (permalink) | ||
|
Forum Novice
Join Date: Jul 2004
Location: Switzerland
Age: 25
Posts: 234
|
Quote:
Quote:
However, if both were AND, this would probably lead to the same performance as it does the same thing (assuming there are no variables that run out of scope). Away from that, you'll find better ways to improve performance than comparing two if's against each other. ----- 1: not sure about that, but under the hood, this should be compiled to just the same code afaik. 2: No, double stores less digits. An integral value like long is stored 'as is'. A floating point value is stored with a mantissa and an exponent to its base. These types are also called 'approximate numbers', as they have a limited precision. You can use double to store much higher values than with int64, but they are of limited precision. Only the higest 16 digits of a number are stored here. 3:decimal has another base. Unlike the other types, it's 10 based, double is 2 based, leading to those wonderful outputs like 0.8 - 0.1 = 0.699999~. And it's not an approximate number. A decimal stores more 'digits' than a double, but less on the left side of the point, it's more precise which is really importent if you're calculating with, say, currencies. 4:1byte is the smallest chunk of memory a processor can handle. Eheres nothing more to say about this, its a technology restriction. Except of: .Net usually pads even 8bit memory to 32bit. 5: No, this is just the same IL code. A difference with this 2 statements is most likley of inaccurate testing - you can't test this only with one run, as other processes on your system can interfer with the test. 6: Try to run it without the F. It won't compile. The F is there to tell the compiler that this constant is a float-type. Without it, the compiler assumes it to be a double and it cant implicitly convert it to float. |
||
|
|
|
|
|
#9 (permalink) | |
|
Forum Expert
|
Code:
if( ((from != null) || (from.Map != Map.Internal)) || (!from.Deleted) )
{
//action code
}
Code:
if( from != null )
{
if( from.Map != Map.Internal )
{
if( !from.Deleted )
{
//action code
}
}
}
The first one performs the action code if: from is not null, or if map is not internal or if from is not deleted. However, the second one: If from isn't null check if the map is internal, if not: check if from is not deleted. For both of them to be the same the first one needs to be changed to: Code:
if((from != null) && (from.Map != Map.Internal) && (!from.Deleted))
{
//action code
}
__________________
Quote:
|
|
|
|
|
|
|
#10 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
|
yeah storm and ray are right. I didnt even look at them cause i knew the kind of question you were asking (There was a thread that went through an argument dealing with them) and what you wanted to know.
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial |
|
|
|
|
|
#11 (permalink) |
|
ConnectUO Creator
Join Date: Jan 2004
Location: In your mom
Age: 27
Posts: 4,762
|
Considering the 1st part of your thread here, the int vs Int32. What I find weird is they are treated the same when you output their Type...
Take the following code Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace SpeedTesting
{
class Program
{
static Stopwatch _stopWatch;
static void Main(string[] args)
{
_stopWatch = new Stopwatch();
for( int b = 0; b < 15; b++ )
{
_stopWatch.Start();
for( int i = 0; i < int.MaxValue; i++ )
{
int a = 0;
}
_stopWatch.Stop();
Output(typeof(int), int.MaxValue, _stopWatch.ElapsedMilliseconds);
_stopWatch.Reset();
_stopWatch.Start();
for( int i = 0; i < int.MaxValue; i++ )
{
Int32 a = 0;
}
_stopWatch.Stop();
Output(typeof(Int32), int.MaxValue, _stopWatch.ElapsedMilliseconds);
_stopWatch.Reset();
Console.WriteLine();
}
Console.ReadLine();
}
private static void Output(Type type, int times, long milliSeconds)
{
Console.WriteLine("Called ({0}) {1} times in {2} milliseconds", type, times, milliSeconds);
}
}
}
However, here is the output of the application... Code:
Called (System.Int32) 2147483647 times in 6729 milliseconds Called (System.Int32) 2147483647 times in 6627 milliseconds Called (System.Int32) 2147483647 times in 6691 milliseconds Called (System.Int32) 2147483647 times in 6666 milliseconds Called (System.Int32) 2147483647 times in 6715 milliseconds Called (System.Int32) 2147483647 times in 6665 milliseconds Called (System.Int32) 2147483647 times in 6668 milliseconds Called (System.Int32) 2147483647 times in 6591 milliseconds Called (System.Int32) 2147483647 times in 6597 milliseconds Called (System.Int32) 2147483647 times in 6542 milliseconds Called (System.Int32) 2147483647 times in 6573 milliseconds Called (System.Int32) 2147483647 times in 6551 milliseconds Called (System.Int32) 2147483647 times in 6764 milliseconds Called (System.Int32) 2147483647 times in 6665 milliseconds Called (System.Int32) 2147483647 times in 6677 milliseconds Called (System.Int32) 2147483647 times in 6641 milliseconds Called (System.Int32) 2147483647 times in 6620 milliseconds Called (System.Int32) 2147483647 times in 6762 milliseconds Called (System.Int32) 2147483647 times in 6766 milliseconds Called (System.Int32) 2147483647 times in 6639 milliseconds Called (System.Int32) 2147483647 times in 6641 milliseconds Called (System.Int32) 2147483647 times in 6629 milliseconds Called (System.Int32) 2147483647 times in 6678 milliseconds Called (System.Int32) 2147483647 times in 6624 milliseconds Now just to make sure there wasnt some odd fluke since i called int first and Int32 second, i reversed the statements as follows Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace SpeedTesting
{
class Program
{
static Stopwatch _stopWatch;
static void Main(string[] args)
{
_stopWatch = new Stopwatch();
for( int b = 0; b < 15; b++ )
{
_stopWatch.Start();
for( int i = 0; i < int.MaxValue; i++ )
{
Int32 a = 0;
}
_stopWatch.Stop();
Output(typeof(int), int.MaxValue, _stopWatch.ElapsedMilliseconds);
_stopWatch.Reset();
_stopWatch.Start();
for( int i = 0; i < int.MaxValue; i++ )
{
int a = 0;
}
_stopWatch.Stop();
Output(typeof(Int32), int.MaxValue, _stopWatch.ElapsedMilliseconds);
_stopWatch.Reset();
Console.WriteLine();
}
Console.ReadLine();
}
private static void Output(Type type, int times, long milliSeconds)
{
Console.WriteLine("Called ({0}) {1} times in {2} milliseconds", type, times, milliSeconds);
}
}
}
Code:
Called (System.Int32) 2147483647 times in 6768 milliseconds Called (System.Int32) 2147483647 times in 6658 milliseconds Called (System.Int32) 2147483647 times in 6660 milliseconds Called (System.Int32) 2147483647 times in 6648 milliseconds Called (System.Int32) 2147483647 times in 6765 milliseconds Called (System.Int32) 2147483647 times in 6651 milliseconds Called (System.Int32) 2147483647 times in 6679 milliseconds Called (System.Int32) 2147483647 times in 6672 milliseconds Called (System.Int32) 2147483647 times in 6686 milliseconds Called (System.Int32) 2147483647 times in 6693 milliseconds Called (System.Int32) 2147483647 times in 6622 milliseconds Called (System.Int32) 2147483647 times in 6659 milliseconds Called (System.Int32) 2147483647 times in 6763 milliseconds Called (System.Int32) 2147483647 times in 6632 milliseconds Called (System.Int32) 2147483647 times in 6565 milliseconds Called (System.Int32) 2147483647 times in 6649 milliseconds Called (System.Int32) 2147483647 times in 6687 milliseconds Called (System.Int32) 2147483647 times in 6736 milliseconds Called (System.Int32) 2147483647 times in 6629 milliseconds Called (System.Int32) 2147483647 times in 6598 milliseconds Called (System.Int32) 2147483647 times in 6677 milliseconds Called (System.Int32) 2147483647 times in 6745 milliseconds Called (System.Int32) 2147483647 times in 6617 milliseconds Called (System.Int32) 2147483647 times in 6594 milliseconds ![]()
__________________
Jeff Boulanger ConnectUO - Core Developer Want to help make ConnectUO better? Click here to submit your ideas/requests Use your talent to compete against other community members in RunUO hosted coding competitions If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team. Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO |
|
|
|
|
|
#12 (permalink) | ||
|
Forum Expert
|
Quote:
It all seemed pretty interesting, I never thought to stop and think about performance from doing the same thing in such small amounts of ways (as shown in my first post, it's a quote from another site...). Quote:
![]() What thread?
__________________
|
||
|
|
|
|
|
#13 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
|
It had to do with the way if statements worked and how combining all the ifs into one line (like your example) would throw an exception when the object is null...
let me look for it... Edit: I think this was it nullchecking - is this safe? but it actually dealt with a problem from another thread (which i think this thread has a link to it), but we argued the point in this one
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial Last edited by mordero; 02-12-2007 at 01:03 PM. |
|
|
|
|
|
#14 (permalink) | |
|
Forum Expert
|
Quote:
![]() I never knew that the "is" statement checked if it were null first... Like if monster were some random monster, and it were deleted and then this statement gets thrown: if( monster is PlayerMobile ) it still wont throw a crash. I've always been checking if it's null before doing something like that! lol. Bleh! :P Well, now I know one more thing! ![]() Oh well, very good info that one is... actually this entire discussion is full of good, and highly interesting information. PS. Great program Jeff, kind of makes one wonder about it even more-so too!
__________________
|
|
|
|
|
|
|
#15 (permalink) |
|
Forum Expert
|
@Jeff
I can't be completely sure on this, but my guess to as why int and Int32 are both coming out as System.Int32 is because they are actually the same thing. I believe that int is just an alias for Int32, probably to keep some of the feel of C++. I bet that when the compiler generates the IL code for int, it is the same as Int32. Matter of fact, I just tested it out now and this is what I wrote: Code:
using System;
class Program
{
static void Main( string[] args )
{
Int32 a = 0;
int b = 0;
Console.WriteLine( "{0}, {1}", a, b );
}
}
Code:
using System;
internal class Program
{
private static void Main(string[] args)
{
int num1 = 0;
int num2 = 0;
Console.WriteLine("{0}, {1}", num1, num2);
}
}
Now... as for the timing differences. I think that on the scale of only 15 iterations, the output could seem a little fishy. Consider the following: Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace SpeedTesting
{
class Program
{
static Stopwatch _stopWatch;
static List<long> _intClocks;
static List<long> _32Clocks;
static void Main( string[] args )
{
_stopWatch = new Stopwatch();
_intClocks = new List<long>();
_32Clocks = new List<long>();
for ( int b = 0; b < 100; b++ )
{
_stopWatch.Start();
for ( int i = 0; i < 100000000; i++ )
{
int a = 0;
}
_stopWatch.Stop();
_intClocks.Add( _stopWatch.ElapsedMilliseconds );
_stopWatch.Reset();
_stopWatch.Start();
for ( int i = 0; i < 100000000; i++ )
{
Int32 a = 0;
}
_stopWatch.Stop();
_32Clocks.Add( _stopWatch.ElapsedMilliseconds );
_stopWatch.Reset();
}
DumpLists();
Console.WriteLine( "done" );
Console.ReadLine();
}
static void DumpLists()
{
using ( System.IO.StreamWriter op = new System.IO.StreamWriter( "output.log", true ) )
{
op.WriteLine( "Int32 vs. int:\n" );
long intTotal = 0;
long i32Total = 0;
for ( int i = 0; i < _intClocks.Count; i++ )
{
intTotal += _intClocks[i];
i32Total += _32Clocks[i];
}
op.WriteLine( "Average time for int: {0}", intTotal / _intClocks.Count );
op.WriteLine( "Average time for Int32: {0}\n", i32Total / _32Clocks.Count );
op.WriteLine( "//++++++++++++++++ int ++++++++++++++++//" );
for ( int i = 0; i < _intClocks.Count; i++ )
op.WriteLine( _intClocks[i] );
op.WriteLine( "//++++++++++++++++ Int32 ++++++++++++++++//" );
for ( int i = 0; i < _32Clocks.Count; i++ )
op.WriteLine( _32Clocks[i] );
}
}
}
}
Here is my output.log: Code:
Int32 vs. int: Average time for int: 89 Average time for Int32: 88 //++++++++++++++++ int ++++++++++++++++// 164 88 86 86 86 86 86 85 90 89 89 88 88 88 85 111 120 118 86 88 85 91 90 87 84 84 88 89 87 87 87 90 86 91 112 155 90 85 86 82 88 88 83 85 89 88 84 88 87 86 89 91 84 85 86 89 84 88 91 85 88 89 84 83 88 85 87 88 85 88 87 84 84 88 87 84 91 86 88 86 90 90 87 89 87 87 86 85 86 86 87 87 85 87 86 86 91 90 84 86 //++++++++++++++++ Int32 ++++++++++++++++// 108 87 88 84 87 87 91 88 89 89 90 86 84 86 88 118 117 86 86 84 86 88 86 86 87 87 88 86 88 87 87 86 86 86 162 160 86 89 88 85 83 83 87 87 87 83 85 87 85 86 86 85 84 86 87 89 85 85 84 87 86 85 86 86 89 86 86 90 90 83 86 87 85 85 86 88 84 88 86 86 87 88 88 90 88 89 86 86 88 85 90 85 84 84 88 87 85 85 86 86 Do you agree? |
|
|
|
|
|
#16 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 21
Posts: 2,911
|
nicely done milt
![]()
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial |
|
|
|
|
|
#17 (permalink) | |
|
Forum Novice
Join Date: Jul 2004
Location: Switzerland
Age: 25
Posts: 234
|
Quote:
There are countless reasons why 2 statements of the same type perform with a slightly different speed. Like the scheduler. Every thread on your system gets a slice of CPU time in which it can perform instructions. With a look at the TaskMan, you can see that there are hundred of threads working simultaneously. Everyone of them aquires a small slice of CPU time which can lead to a major delay, if the foreign thread does some intensive calculations. You can virtually surpass this, if you set the priority of your own application to 'real time', but this could cause system instability. An endless loop or lock in a realtime application probably forces you to restart your system with the good old power button. Or the common runtime environment, like the garbage collector. As you can't control how and when the GC performs the task, it can pretty easily kill such performancetests. As milt proved, the C# compiler translates every int to an Int32 type, this is because CIL doesnt know a Type 'int', this is just a keyword in c# that refers to the real Int32 class. Same with every other of the so-called built-in types. Even within other languages like VB.Net (Integer) It's quite an interesting topic how the standard implementation perform against each other, but the real gain of performance lies somewhere else. There are some rules-of-thumb to use in this very order...
IfElse mostly plays a part in this game, so here are some more tipps to them:
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|