How do I choose the correct primative?

TheFlyingFiddle kurtyan at student.chalmers.se
Wed Jan 1 17:50:17 PST 2014


> I'm a little OCD - who cares about memory to that degree 
> anymore when we have gigabytes of RAM? This might not even come 
> into play on the Raspberry Pi.

Memory is very important when it comes to performance, the moving 
of memory is the single most energy demanding task the CPU (and 
the GPU for that matter) has.
See
(http://channel9.msdn.com/Events/Build/2013/4-329 and
  http://media.medfarm.uu.se/play/video/3261 for why this is so)

Anyhow if you want to get a good understanding about how memory 
works and is related to performance i would highly recommend 
reading this entire PDF 
http://www.akkadia.org/drepper/cpumemory.pdf

>>Then I took Architecture & Assembly class. There I learned that 
>>the load instruction grabs an entire native word size, every 
>>time, regardless of how many bits your variable takes up.

>>When we programmed in assembly in that class, for both 
>>performance and coding ease, we only worked with variables that 
>>were the native code size.

Keep in mind that schools are usually 5-15 years behind current 
technology (especially introductory classes) and what was true 
then of performance does not have to be true today.

>>I found out that it's actually extra work for the processor to 
>>use values smaller than the native word size: it has to AND off 
>>the unwanted bits and possibly shift them over.

>>So, if dmd packs variables together, I would want to always use 
>>the native word size to avoid that extra work, and I would 
>>never want to use shorts, ints, or longs. Instead, I'd want to 
>>do this:

This "extra work" is highly unlikely to be your bottleneck. Also 
don't assume that making everything be of native size is going to 
make things faster just because of an AND/SHIFT instruction.

>>And then only use natives, unatives, and booleans (can't avoid 
>>them) for my primatives.

You can use normal integers in if statements if you want. 
Anything not 0 will be true.

int flag = someFunc();
if(flag)
{
    //Do something.
}

>>I really hope this isn't the case because it would make D's 
>>entire primative system pointless. In acedamia, C is often 
>>scolded for its ints always being the native word size, while 
>>Java is praised for being consistent from platform to platform. 
>>But if dmd packs its variables, D is the one that should be 
>>scolded and C is the one that should be praised for the same 
>>reason of the opposite.

D is like java.
(u)byte  is 8bit
(u)short is 16bit
(u)int   is 32bit
(u)long  is 64bit

size_t and pttdiff_t are of platform size.

Note that floats are always calculated by the systems highest 
precision.
float  32bit
double 64bit
real   platform dependant but never lower then 64bit.


//So, I need to have this question answered to even get past the
//function prototype stage - each answer would result in different
//code.

You should not care about this in the function prototype stage 
this is the essence of premature optimization. You cant be sure 
of how fast something is going to be before you profile it. And 
even then it will vary from run to run / computer to computer and 
compiler to compiler. I think this book gives a nice introduction 
to the subject. 
http://carlos.bueno.org/optimization/mature-optimization.pdf


More information about the Digitalmars-d-learn mailing list