int Total = 49;
int Class = 24;
int Eat = 1;
int First = Total - Class;
int Second = First - Eat;
int Main = Total / Second;
Console.Write(Main);
Console.ReadLine();
//Answer = 2
That looks like a way to do it, But remember a int is far far far too big for what your trying to-do, The max-value of an int is:
where as a byte only has a max value of 255, You wont be going over 255, You could even use a sbyte which has a max value of 127, And looking at your calculations you wont be going over 127 either; sbyte would be the fastest and most effective; I don't remember if byte and/or sbyte can be applied to / and - without doing -= and /=.
"Fastest" is nonsense when talking about the CLR, since there's only one instruction for each arithmetic operation (add, sub, mul, div etc), and it works on a word sized integer. That means when a byte is pushed onto the CLR stack, it's being put into a 32/64 bit reg/address which the arithmetic instruction operates on. This might actually be slower, since there could be additional instructions to pad the data. (Eg, MOVZX used instead of MOV on x86). The difference will be negligible anyway, but you can say for certain there is no performance benefit to using bytes.
Using byte rather than int is only particularly important is you're storing arrays of them, to reduce the amount of memory you need to consume. It's pretty pointless to use them as local method variables, or for storage of single bytes (since the CLR will usually pad a structure to a word size boundary anyway).
The same is mostly true for floats and doubles on a 64bit CPU, as every float is emulated as a double anyway.
Does the compiler not put the int into the memory? And gives it the required space that an int uses but if your assigning stuff under 127(sbyte) Would it not be best to announce an sbyte and gives it the required memory which is smaller and presumably uses less memory to make the space for a sbyte then an int. Again, Yes, Nothing noticeable but say you used ulong for every single thing in a program, changing them all to the required variable would save about a second upon loading? no?
The point I was trying to make clear is that, even if you use bytes, you are not saving any space - the compiler will treat them as ints for most things. (With the exception of array of bytes, or collections of bytes in structs). When you have a single byte, it will occupy 4 bytes because of the way the compiler aligns data to memory words.
And note there that I'm talking about storage (such as fields in a class). When talking about local variables, they are almost entirely treated as ints for their short lifetime. Size is really not an issue here, since the memory is only occupied for the duration of the method.
The CLR stack is a fixed width of ints though, so all those ldloc and stloc are moving ints around in memory - not bytes. We can see this by dissassembling it and snipping out the relevant parts.
All instructions are using dwords (int). The ASM isn't optimised to use smaller register sizes like AL, as you might've expected. Instead, the conv.u1 instruction is simply converted to (& 0xFF), removing the overfill of a byte.
If you convert the original C# to use int instead of byte - the difference will be the conv.u1 instruction is gone, and the matching and eax, 0xff CPU instruction with it. There's actually no other difference: they are treated equally as ints, and using bytes is doing nothing but adding extra instructions.
Of course, there are meaningful times when it's desirable to use bytes, if you are specifying the intention that something should be a byte. But if you are performing arithmetic on a value, and you want to use bytes purely for the sake of "saving space", you're wasting instructions.
In any language using "int" or the common size of the registers on your CPU is the fastest (and most compilers will optimize it like that anyways).
That's an over-generalized assumption and not particularly true. Native code compilers won't always widen integers, because there are far more important optimisations like register allocation - where you wanna fit as many values into your limited registers as possible (because reading from memory is slow). On x86, it's no slower to use ADD AL than it is to use ADD EAX, but it is slower to use MOVZX EAX than to use MOV EAX.
On other architectures like ARM though, you can't necessarily access parts of registers (and it wouldn't make sense anyway, since you can switch the endianness of them), then widening to ints is often the simplest and quickest way to do things.
Those are implementation specific optimisations made by compilers anyway. The point I was trying to convey is that the CLI standard explicitly defines that widening occurs, and that all implementations of it should do so. It's done for simplication and portability of the runtime, but not necessarily optimisation (and quite clearly, it limits how much optimisation can be done).
Quote:
Originally Posted by BaussHacker
Isn't an integer default in C#?
Quote:
Originally Posted by ECMA-335$12.1.2
Loading from 1- or 2-byte locations (arguments, locals, fields, statics, pointers) expands to 4-byte
values. For locations with a known type (e.g., local variables) the type being accessed determines
whether the load sign-extends (signed locations) or zero-extends (unsigned locations).
Programming For Idiots (Tutorial) - In C# 05/25/2013 - CO2 Programming - 188 Replies This tutorial has been superseded by: http://www.elitepvpers.com/forum/co2-programming/88 5939-programming-idiots-c-take-2-a.html
Well for the next week (or so) due to being bored, I've started a video-tutorial called C# Programming for Idiots and yeah, by the end of it I'll demonstrate how to make a conquer server (patch 5016 and before) without the need of mysql, or anything like that; all you need is the Visual C# compiler, and WinRAR (To extract the videos).
Visual C# Compiler:...
Programming for Idiots (Tutorial) C++ 06/11/2012 - CO2 Programming - 20 Replies Yeah, so in my relation to my C# one (http://www.elitepvpers.com/forum/co2-guides-templa tes/148675-programming-idiots-tutorial-c.html), which wasn't the best I felt I'll *try* make for it with this one. I've also got some spare time in between school, from when exams start, and a new Warlords server comes out... so yeah.
Right, so before I post any links to the videos I'd like to point out, so I don't get bitched at by people saying, "this isn't real C++", most of the things I do will be...