RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

sbyte or int ?

ulatek

Wanderer
sbyte or int ?

Ok, Im trying to learn c#. Just got started really..
This may be a stupid question..

But here goes, if im initializing/declaring a variable thats small, like 20 or something. Should I use sbyte or something like that instead of int? Im just asking because Im reading about it, and in this chart it says size in bytes..and sbyte uses less. But I notice in most sources ive looked at they almost always use int. Just wondering if using sbyte or whatever would help save on memory usage.. I obviously dont know much about this so excuse me if my question doesnt make any sense ::rolleyes:
 

stormwolff

Knight
ulatek;705686 said:
Ok, Im trying to learn c#. Just got started really..
This may be a stupid question..

But here goes, if im initializing/declaring a variable thats small, like 20 or something. Should I use sbyte or something like that instead of int? Im just asking because Im reading about it, and in this chart it says size in bytes..and sbyte uses less. But I notice in most sources ive looked at they almost always use int. Just wondering if using sbyte or whatever would help save on memory usage.. I obviously dont know much about this so excuse me if my question doesnt make any sense ::rolleyes:

In relation to RunUO it really shouldn't matter due to the relative smaller size of most common "scripts" or features you would code.
 

daat99

Moderator
Staff member
ulatek;705686 said:
Ok, Im trying to learn c#. Just got started really..
This may be a stupid question..

But here goes, if im initializing/declaring a variable thats small, like 20 or something. Should I use sbyte or something like that instead of int? Im just asking because Im reading about it, and in this chart it says size in bytes..and sbyte uses less. But I notice in most sources ive looked at they almost always use int. Just wondering if using sbyte or whatever would help save on memory usage.. I obviously dont know much about this so excuse me if my question doesnt make any sense ::rolleyes:

As for most programs it won't make any noticeable difference if you choose sbyte or int.
I personally would use int because it's more "natural" for the cpu and it's unlikely to cause lack of memory problems (in most of the common programs).

Basically when you use sbyte instead of int the cpu needs to do extra work while it use less memory.
When you use int the cpu does less work (int size is 32bit which is the cpu "word" size as well) but use more memory.

Considering most common computers come with at least 512Mb of memory (that's 536,870,912 bytes) wasting 3 bytes for that particular variable seems like a drop in the ocean.



P.S.
Please send me a pm with more appropriate name for this thread, something that will help people find it using the search, thank you.
 

remnant

Wanderer
You're integers should be the size of the CPU, for instance, if you are running a 32-bit processor, then it is faster to use 32 bit integers (int) rather than a smaller type (like sbyte). This is because the CPU retrieves the data in 32 bit chunks at a time.

As I understand it though, say you have a class that is going be used extensively (like Item for instance). Every time a new instance of Item is created, memory is being allocated for all the variables in the class. If you have 100 int's inside of the Item class, and you have 100,000 Item's on your server, then that would be 32bits * 100Ints * 100,000Items = 320,000,000 bits (or 400,00,000 bytes ) of memory. A gigabyte is 1,073,741,824 bytes, so this would be a good chunk of memory. If you were to use sbytes instead, then we are talking about 10,000,000 as opposed to 400,000,000 bytes.

Perhaps I'm missing something to do with how it is allocated, but this is how I have always understood it. Seems like it would have to work this way in order to preserve data.

Correct me if I'm wrong.
 

daat99

Moderator
Staff member
remnant;707884 said:
You're integers should be the size of the CPU, for instance, if you are running a 32-bit processor, then it is faster to use 32 bit integers (int) rather than a smaller type (like sbyte). This is because the CPU retrieves the data in 32 bit chunks at a time.

As I understand it though, say you have a class that is going be used extensively (like Item for instance). Every time a new instance of Item is created, memory is being allocated for all the variables in the class. If you have 100 int's inside of the Item class, and you have 100,000 Item's on your server, then that would be 32bits * 100Ints * 100,000Items = 320,000,000 bits (or 400,00,000 bytes ) of memory. A gigabyte is 1,073,741,824 bytes, so this would be a good chunk of memory. If you were to use sbytes instead, then we are talking about 10,000,000 as opposed to 400,000,000 bytes.

Perhaps I'm missing something to do with how it is allocated, but this is how I have always understood it. Seems like it would have to work this way in order to preserve data.

Correct me if I'm wrong.
That's correct but let's get this into perspective for a second.
An average RunUO doesn't have more than 10 variables beside the standard variables that you can't control (most only have 1 or 2).
Also an average custom RunUO script doesn't have more than 1000 instances of that class in the shard (usually they won't get even 100 instances).
If you use those more realistic figures than you get the following memory usage (on paper):
10 (ints) * 4 (bytes per int) * 1000 (instances of that item) = 40,000byte = 39.06kb = 0.038mb.
Now if we get those integers into sbytes we get:
10 (sbytes) * 1 (bytes per sbyte) * 1000 (instances of that item) = 10,000byte = 9.76kb = 0.009mb.
As you can see you saved less than 30kb of memory for over 1000 items while reducing the cpu speed when reading/writing sbyte instead of 32bit int (which is what most cpu's and operating systems really use at this time).

Furthermore, in the near future where 64bits systems will be more common and have a stable working operating system that doesn't hook the resources most programmers will use 64bit integers instead of the 32bit integers.

In general, if you wrote something for RunUO that require you to think about saving 3 bytes of memory by using sbyte instead of int than it's safe to assume that you can write it a lot better without converting the integers into sbytes (aside from the devs that need to consider more important issues in the core itself rather than an add on script).
 

Jeff

Lord
remnant;707884 said:
You're integers should be the size of the CPU, for instance, if you are running a 32-bit processor, then it is faster to use 32 bit integers (int) rather than a smaller type (like sbyte). This is because the CPU retrieves the data in 32 bit chunks at a time.
Its not faster, sbyte would be sent as itself inside a 32bit stream. The speed is the same.

remnant;707884 said:
320,000,000 bits (or 400,00,000 bytes ) of memory.
40,000,000 bytes. <-- this is only 38mb of memory, which is nothing.

remnant;707884 said:
A gigabyte is 1,073,741,824 bytes, so this would be a good chunk of memory. If you were to use sbytes instead, then we are talking about 10,000,000 as opposed to 400,000,000 bytes.

The point is, this is for scripabilty. Scripts should be kept simple for extensability. Simple = bool, int, string. Anything else really isnt needed.
remnant;707884 said:
Perhaps I'm missing something to do with how it is allocated, but this is how I have always understood it. Seems like it would have to work this way in order to preserve data.

Correct me if I'm wrong.
Ya your right with allocation. Keeping memory usage to a minimum is all fine and dandy for things like the core, or for other programs, but scripts are scripts.
 

Varchild

Wanderer
Very Interesting post.

^_^

Personally I've ever used only basic c# types (neither float or decimal but only int, double, bool etc ).

A little question: what does this code means?

Code:
BlaBlaBla	= 1 << 0,

Or
Code:
 (r << 16) | (g << 8) | (b << 0);

And also:
Code:
c & 0x7FFF;

I know all of them concern about bytes but don't understand exactly what they mean. Couldyou help me ?

^_^
 

daat99

Moderator
Staff member
Varchild;708394 said:
Very Interesting post.

^_^

Personally I've ever used only basic c# types (neither float or decimal but only int, double, bool etc ).

A little question: what does this code means?

Code:
BlaBlaBla	= 1 << 0,

Or
Code:
 (r << 16) | (g << 8) | (b << 0);

And also:
Code:
c & 0x7FFF;

I know all of them concern about bytes but don't understand exactly what they mean. Couldyou help me ?

^_^

Code:
BlaBlaBla	= 1 << 0,
I believe it means nothing.

I'll use the following sample to demonstrate
Code:
int a = r << 16;
short b = g << 8;
short c = g & 60;
short d = g | 60;
Let's assume variable r is a 32 bit integer and variable g is 16 bit short
r = 150(decimal) or 00000000,00000000,00000000,10010110 (binary)
g = 150(decimal) or 00000000,10010110 (binary)

After the above commands variables a and b will contain the following:
a = 9830400(decimal) or 00000000,10010110,00000000,00000000 (binary)
b = 38400(decimal) or 10010110,00000000 (binary)
c = 20(decimal) or 00000000,00010100 (binary)
d = 190(decimal) or 00000000,10111110 (binary)

Hope that helps clarify the situation.
 

Jeff

Lord
daat99;708460 said:
Code:
BlaBlaBla	= 1 << 0,
I believe it means nothing.

I'll use the following sample to demonstrate
Code:
int a = r << 16;
short b = g << 8;
short c = g & 60;
short d = g | 60;
Let's assume variable r is a 32 bit integer and variable g is 16 bit short
r = 150(decimal) or 00000000,00000000,00000000,10010110 (binary)
g = 150(decimal) or 00000000,10010110 (binary)

After the above commands variables a and b will contain the following:
a = 9830400(decimal) or 00000000,10010110,00000000,00000000 (binary)
b = 38400(decimal) or 10010110,00000000 (binary)
c = 20(decimal) or 00000000,00010100 (binary)
d = 190(decimal) or 00000000,10111110 (binary)

Hope that helps clarify the situation.

Nice example, but you left out something important :) You didnt really explain what it does.

<< and >> are bit shifters. basically you shift the bits of a value left << or right >> the number of times you declare to the right of the shift operator.

If you don't know how binary works, this is kinda confusing, but a simple explination of binary goes like this.

In binary you have 2 values per bit, On = 1, Off = 0. When you have a variable that is declared as a 8 bit value that means you have 8 0's or 1's that form this variable. To easily determine the value of the variable in binary you can read it left to right.

So heres the values of each digit in an 8 bit binary variable

Code:
[FONT="Courier New"]0    0   0   0  0 0 0 0
128 64 32 16 8 4 2 1[/FONT]

If you add all these values up you get the max value of 8bit which is 256. Basically you add every bit's value for the bits that are turned on. So lets say you have the value 35, this would mean the 1 2 and 32 buts were turned on like so.

00100011

So like daat showed but in a way simpler format

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Bitshifting
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;

            Console.WriteLine("a = {0}", a);

            a = a << 1;

            Console.WriteLine("a = {0}", a);

            a = a << 1;

            Console.WriteLine("a = {0}", a);

            a = a >> 2;

            Console.WriteLine("a = {0}", a);
            Console.ReadLine();
        }
    }
}

The output:

Code:
a = 1
a = 2
a = 4
a = 1

As for other binary operators we have

| which means OR
& which means AND
^ which means XOR

With these you compare binary values against each other. Its hard to explain so I'm just going to show some examples with 4 bit variables to make things simpler.

Lets say we have these values

12 = 1100
10 = 1010

if you were to OR these values the resulting value's bits are on where the bits from the 2 values are on

1100
1010 |
------
1110 = 14

See how the result only has a 1 in the places where one of the variables had a 1.

Lets look at AND

AND returns a result where the compared bits between the values are 1's

1100
1010 &
------
1000 = 8

Lets look at XOR

XOR returns a result where the compared bits between the values are not both 1's or both 0's

1100
1010 ^
------
0110 = 6

Hope this adds to the help.
 
Top