473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

time vs. space tradeoffs of primitive types

I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.

Nov 23 '06 #1
10 1722

Noah Roberts wrote:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.
I think it depends on the architecture .. for example on a 32-bit
system there's no speed/time penalty in using an int vs a short for
example .. the proccessor will fetch a 32-bit value anyway

Nov 23 '06 #2

Noah Roberts wrote:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.
Performance penalties depends on the context and the platform. A
primitive type has a range, a min and max value as well as a few more
interesting constants. You can dissect the various qualities of a
primitive using the <limitsheader. Note that performance = 0 if the
primitive you choose generates an overflow. So before you get concerned
with speed, consider the ramifications.

#include <limits>

template< typename T >
void Numerics( const T& r_t )
{
std::cout << "\ntype: " << typeid(r_t).name();
bool is_int = std::numeric_limits< T >::is_integer;
std::cout << "\ninteger = " << is_int;
bool is_signed = std::numeric_limits< T >::is_signed;
std::cout << "\tsigned = " << is_signed;
std::cout << "\tsizeof = " << sizeof(r_t);
T min = std::numeric_limits< T >::min();
std::cout << "\nmin = " << min;
T max = std::numeric_limits< T >::max();
std::cout << "\nmax = " << max;
long capacity = static_cast<long>(max) - min;
std::cout << "\ncapacity = " << capacity;
std::cout << std::endl;
}

int main()
{
int n(0);
Numerics(n);
// etc
}

Nov 23 '06 #3

st***************@gmail.com wrote:
Noah Roberts wrote:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.

I think it depends on the architecture .. for example on a 32-bit
system there's no speed/time penalty in using an int vs a short for
example .. the proccessor will fetch a 32-bit value anyway
It doesn't have to do more processing steps to work with only half of
that value once it fetches it?

Nov 23 '06 #4
Noah Roberts wrote:
[performance and space of different types]

This is all off topic in comp.lang.c++. But here goes anway.

If the differences are going to be important, then the thing to do is
try different ways of doing the task and measure.

Get some way of determining how much RAM a prog takes.
(Platform specific.) See if there is in fact a significant difference.
See if it is in fact in the direction you expected. In some cases
this may not be a trivial task, as operating systems tend to
hide such details from casual users. And you may not see
the difference at all on small applications or small allocations.
Some OS platforms will allocate memory in chunks so as to
avoid a lot of tiny memory fetches.

Set up some typical application task and get out your stop
watch. Be sure to account for various things about your OS
that might give spurious time values. Things like loading, or
the OS decides to do a disk maintenance cycle or something.

Generally speaking, when such performance and size issues
become critical, developers tend to make "tweaked" versions
of the code for each target platform. So, if various compile
options, or variable size, or bounds limits, etc., make a prog
run faster on one platform, that set of options is set up as
a config file for that platform. Then each targeted platform
gets its own config file. Possibly on a complete other
compiler for some targeted platforms.

This has the disagreeable aspect of possibly requiring some
noticeable differences in how apps run, how they appear, etc.
But that is probably unavoidable for some cases. XWindows
and MSWindows, for example, are going to have some code
that simply is not portable.
Socks

Nov 23 '06 #5
Noah Roberts wrote:
st***************@gmail.com wrote:
>Noah Roberts wrote:
>>I understand that different integer types might have time
penalties because of word boundaries. Where can I get
information clarifying this topic? A quick google didn't turn up
much for me...nice little C++ vs. Java troll war on wikipedia but
nothing on what I'm looking for. I want to know whether space
savings using smaller types to represent numbers you know will be
small are ever seen. I would also like to know what kind of
performance penalties are associated with using these smaller
types.

I think it depends on the architecture .. for example on a 32-bit
system there's no speed/time penalty in using an int vs a short for
example .. the proccessor will fetch a 32-bit value anyway

It doesn't have to do more processing steps to work with only half
of that value once it fetches it?
It will probably work on all the bits anyway, but only use half the result.

On the other hand, one very popular 32-bit instruction set requires an extra
size-override byte to do 16-bit operations.

On the third hand :-) smaller data uses less cache space, so it might be
faster.

As usual, it depends.
Bo Persson
Nov 23 '06 #6
On 23 Nov 2006 00:09:30 -0800, "Noah Roberts" <ro**********@gmail.com>
wrote in comp.lang.c++:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.
There are absolutely none at all, at the C++ language level. This is
easily understood by the fact the language standard does not specify
anything about the relative or absolute speed of anything.

And there really can't be any useful information about this on the C++
language level, since it is completely processor and platform
dependent.

If you really need to know this about the code a particular compiler
generates for a particular processor architecture, you need to do one
of the following:

-- consult the documentation for that particular compiler

-- ask in a newsgroup that supports that compiler/processor
combination

-- get a copy of the processor manual, and study execution timings

There are probably other possibilities, but all of them are off-topic
here because they are implementation specific and not language issues.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 24 '06 #7
On 23 Nov 2006 00:30:56 -0800, st***************@gmail.com wrote in
comp.lang.c++:
>
Noah Roberts wrote:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.

I think it depends on the architecture .. for example on a 32-bit
system there's no speed/time penalty in using an int vs a short for
example .. the proccessor will fetch a 32-bit value anyway
While the entire issue is really off-topic here, you have fallen into
the classic trap of answering an off-topic question in a domain where
you are apparently not an expert.

Namely you are just flat-out absolutely wrong.

Consider the two most widely used 32-bit architectures in the world
today, x86 and ARM.

Both have penalties, in the form of either instruction prefixes, extra
clock cycles, or both, for accessing 16-bit variables in memory.

Sometimes pipelining hides the effects of the extra work, sometimes
not, but it is always there.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 24 '06 #8
On Nov 23, 9:21 pm, "Bo Persson" <b...@gmb.dkwrote:
Noah Roberts wrote:
stefan.constan...@gmail.com wrote:
Noah Roberts wrote:
I understand that different integer types might have time
penalties because of word boundaries. Where can I get
information clarifying this topic? A quick google didn't turn up
much for me...nice little C++ vs. Java troll war on wikipedia but
nothing on what I'm looking for. I want to know whether space
savings using smaller types to represent numbers you know will be
small are ever seen. I would also like to know what kind of
performance penalties are associated with using these smaller
types.
I think it depends on the architecture .. for example on a 32-bit
system there's no speed/time penalty in using an int vs a short for
example .. the proccessor will fetch a 32-bit value anyway
It doesn't have to do more processing steps to work with only half
of that value once it fetches it?It will probably work on all the bits anyway, but only use half the result.

On the other hand, one very popular 32-bit instruction set requires an extra
size-override byte to do 16-bit operations.

On the third hand :-) smaller data uses less cache space, so it might be
faster.
Unless there is 32-bit alignment and/or the cache-lines are larger than
the data. As a general rule of thumb I'd say that you should go with
the "native" size, since the processors is usually optimized for these.
As a second rule of thumb I'd say that you'll almost always will have
to trade in performance to get smaller footprint.

--
Erik Wikström

Nov 24 '06 #9

Noah Roberts wrote:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.
Not sure about alignment issues, but IIRC any 'small' int(signed char,
short etc) is converted to an int before math ops ( section 4.5, 5.9 in
the C++ standard for more details) IOW for performance in calcs (In
theory at least) I can't see a gain and maybe there will be a loss due
to conversion from using smaller types than ints.

regards
Andy Little

Nov 24 '06 #10
er****@student.chalmers.se wrote:
On Nov 23, 9:21 pm, "Bo Persson" <b...@gmb.dkwrote:
>Noah Roberts wrote:
>>stefan.constan...@gmail.com wrote:
Noah Roberts wrote:
I understand that different integer types might have time
penalties because of word boundaries. Where can I get
information clarifying this topic? A quick google didn't turn
up much for me...nice little C++ vs. Java troll war on
wikipedia but nothing on what I'm looking for. I want to know
whether space savings using smaller types to represent numbers
you know will be small are ever seen. I would also like to
know what kind of performance penalties are associated with
using these smaller types.
>>>I think it depends on the architecture .. for example on a 32-bit
system there's no speed/time penalty in using an int vs a short
for example .. the proccessor will fetch a 32-bit value anyway
>>It doesn't have to do more processing steps to work with only half
of that value once it fetches it?It will probably work on all the
bits anyway, but only use half the result.

On the other hand, one very popular 32-bit instruction set
requires an extra size-override byte to do 16-bit operations.

On the third hand :-) smaller data uses less cache space, so it
might be faster.

Unless there is 32-bit alignment and/or the cache-lines are larger
than
the data. As a general rule of thumb I'd say that you should go with
the "native" size, since the processors is usually optimized for
these.
As a second rule of thumb I'd say that you'll almost always will
have
to trade in performance to get smaller footprint.
Sure, I was assuming that the OP was using *a lot* of these variables. If
they are half the size, each cache line will hold twice the amount. If he
uses only one or two, it doesn't matter at all.
Bo Persson
Nov 25 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Glen Wheeler | last post by:
Hello all, My program uses many millions of integers, and Python is allocating way too much memory for these. I can't have the performance hit by using disk, so I figured I'd write a C...
3
by: Edg Bamyasi | last post by:
This Is A Late Cross Post from comp.lang.python. It seems the mistery is deeper then i expected. What is the running time of conactination on character strings. i.e. >> joe="123" >>...
3
by: Tony Johansson | last post by:
Hello! I'm reading in a document about window system and here they talk about primitive class. So my question is what is a primitive class? It might not mean any spacial just a name of a...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
26
by: Kip | last post by:
Greetings everyone, Is there anyone here who could point me to a page or pdf that has a list of the sizes of all of the C primitive data types on various implementations such as SPARC, x86,...
3
by: Bear | last post by:
Hello there How come it's possible to add values of the type "int" into an ArrayList when the Add member function only accepts values of type "object"? Is an int just a "typedef" of the Int32...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
1
by: Neil Zanella | last post by:
Hello all, In C and C++ a primitive data type is represented by a minimum number of bits as defined by the corresponding standard. For instance an int is assumed to be at least 16 bits on all...
4
by: jehugaleahsa | last post by:
Hello: When developing data structures for C#, there is an obvious performance hit when utilizing primitive types. For instance, a recent hash table implementation I wrote works exceedingly fast...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.