Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 08-21-2007, 08:42 AM   #1 (permalink)
Newbie
 
ulatek's Avatar
 
Join Date: Aug 2003
Posts: 39
Default 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 :
ulatek is offline   Reply With Quote
Old 08-21-2007, 10:24 AM   #2 (permalink)
Forum Expert
 
stormwolff's Avatar
 
Join Date: Nov 2003
Location: The Internet
Age: 28
Posts: 3,510
Default

Quote:
Originally Posted by ulatek View Post
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 :
In relation to RunUO it really shouldn't matter due to the relative smaller size of most common "scripts" or features you would code.
stormwolff is offline   Reply With Quote
Old 08-21-2007, 05:41 PM   #3 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by ulatek View Post
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 :
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.
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 08-21-2007, 09:28 PM   #4 (permalink)
Newbie
 
ulatek's Avatar
 
Join Date: Aug 2003
Posts: 39
Default

Thanks for clearing that up
ulatek is offline   Reply With Quote
Old 08-22-2007, 01:27 AM   #5 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by ulatek View Post
Thanks for clearing that up
Glad I could help
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 08-29-2007, 07:53 PM   #6 (permalink)
Forum Expert
 
Join Date: Sep 2004
Location: Florida
Age: 25
Posts: 457
Send a message via AIM to remnant
Default

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.
remnant is offline   Reply With Quote
Old 08-30-2007, 05:14 PM   #7 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by remnant View Post
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).
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 08-30-2007, 05:23 PM   #8 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 28
Posts: 4,886
Default

Quote:
Originally Posted by remnant View Post
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.

Quote:
Originally Posted by remnant View Post
320,000,000 bits (or 400,00,000 bytes ) of memory.
40,000,000 bytes. <-- this is only 38mb of memory, which is nothing.

Quote:
Originally Posted by remnant View Post
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.
Quote:
Originally Posted by remnant View Post
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.
__________________
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
Jeff is offline   Reply With Quote
Old 08-30-2007, 08:25 PM   #9 (permalink)
Forum Expert
 
Join Date: Sep 2004
Location: Florida
Age: 25
Posts: 457
Send a message via AIM to remnant
Default

Well, thanks for clarification.
remnant is offline   Reply With Quote
Old 09-01-2007, 06:24 AM   #10 (permalink)
Forum Novice
 
Varchild's Avatar
 
Join Date: Jan 2004
Location: Gorizia - Italy
Age: 28
Posts: 193
Send a message via ICQ to Varchild Send a message via MSN to Varchild
Default

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 ?

^_^
__________________
Dies Irę, dies illa
solvet sęclum in favilla

Developer only on
Midgard Shard
Varchild is offline   Reply With Quote
Old 09-01-2007, 06:12 PM   #11 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by Varchild View Post
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.
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 09-02-2007, 05:13 PM   #12 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 28
Posts: 4,886
Default

Quote:
Originally Posted by daat99 View Post
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:
0    0   0   0  0 0 0 0
128 64 32 16 8 4 2 1
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.
__________________
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
Jeff is offline   Reply With Quote
Old 09-03-2007, 06:54 AM   #13 (permalink)
Forum Novice
 
Varchild's Avatar
 
Join Date: Jan 2004
Location: Gorizia - Italy
Age: 28
Posts: 193
Send a message via ICQ to Varchild Send a message via MSN to Varchild
Default

Thank u all for a so detailed infos.
I've learnt something new, thanks again.

^_^
__________________
Dies Irę, dies illa
solvet sęclum in favilla

Developer only on
Midgard Shard
Varchild is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5