|
||
|
|
#1 (permalink) |
|
Newbie
Join Date: Aug 2003
Posts: 39
|
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 : ![]() |
|
|
|
|
|
#2 (permalink) | |
|
Forum Expert
Join Date: Nov 2003
Location: The Internet
Age: 28
Posts: 3,510
|
Quote:
|
|
|
|
|
|
|
#3 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
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 ------------------------------------------------------------- |
|
|
|
|
|
|
#5 (permalink) |
|
RunUO Forum Moderator
|
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
#6 (permalink) |
|
Forum Expert
|
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. |
|
|
|
|
|
#7 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
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 ------------------------------------------------------------- |
|
|
|
|
|
|
#8 (permalink) | ||
|
ConnectUO Creator
Join Date: Jan 2004
Age: 28
Posts: 4,886
|
Quote:
40,000,000 bytes. <-- this is only 38mb of memory, which is nothing. Quote:
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 |
||
|
|
|
|
|
#10 (permalink) |
|
Forum Novice
|
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, Code:
(r << 16) | (g << 8) | (b << 0); Code:
c & 0x7FFF; ^_^ |
|
|
|
|
|
#11 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
Code:
BlaBlaBla = 1 << 0, 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; 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 ------------------------------------------------------------- |
|
|
|
|
|
|
#12 (permalink) | |
|
ConnectUO Creator
Join Date: Jan 2004
Age: 28
Posts: 4,886
|
Quote:
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 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();
}
}
}
Code:
a = 1 a = 2 a = 4 a = 1 | 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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|