2

I've been searching for examples via Google searches and here on StackOverflow, and I still haven't found anything conclusive to satisfy my understanding.

I've seen posts about the differences between the types and what they are, but nothing conclusive on when I should prefer to use one over the other...

For my current project, I'm working on a Matrix class that will be used to perform the calculations for a Neural Network in the field of A.I. programming and I'm looking for fast calculations with a high probability to utilize AVX instructions or other Vector type registers...

consider the following code snippet:

#incluce <cstdint>
#include <limits>
#include <numeric>

class Matrix64 {
    std::int64_t rows_, columns_;
    std::int64_t size_;

public:
    Matrix64(std::int64_t rows, std::int64_t cols)
      : rows_{rows}, cols_{cols}, size_{rows*cols}
    {}
};

class MatrixFast64 {
    std::int_fast64_t rows_, columns_;
    std::int_fast64_t size_;

public:
    MatrixFast64(std::int_fast64_t rows, std::int_fast64_t cols)
      : rows_{rows}, cols_{cols}, size_{rows*cols}
    {}
};

Which of the two-class examples would be the appropriate choice? When should one type be preferred over the other? This question is centered around the context of knowing when it is appropriate to use one or the other. I feel that this is important when making design decisions.

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 2
    if you want it to be fast, it sounds like you should use the second implementation. Honestly though, since it is 64 bit on a probably 64 bit architecture, I would guess this code compiles to be the same. But your intent is very clear to the user, they shouldn't be surprised if they make one of these and it has more memory usage then 3 * 64 bits ... – Fantastic Mr Fox Jul 15 '20 at 06:10
  • @FantasticMrFox Okay that makes sense... It's one of those things where you just want clarity! – Francis Cugler Jul 15 '20 at 06:11
  • @FantasticMrFox I kind of have another question directly related to this, but I really don't want to make another post just for it... How would this affect `alignment` and `boundaries` within a structure or a class? Although the later is declared to be `fast` the previous is guaranteed to be exactly 64bit and would always have the same alignment... – Francis Cugler Jul 15 '20 at 06:15
  • usually nobody bothers because the names are ugly and, it seems unlikely there will be a system with larger-than-64bit types but not 64-bit types – M.M Jul 15 '20 at 06:18
  • 2
    I don't see how the question is a duplicate. I've found and read that first Q/A and it still didn't give me the clarity that I wanted. I wasn't asking what the differences were. I was asking when it is appropriate to use one or the other within a specific context or domain. – Francis Cugler Jul 15 '20 at 06:20
  • 1
    Rule of thumb for me is `intX_t` if I need a type to be precisely `X` bit (for example when interfacing with graphics hardware that expects 32-bit int values) and `int_fastX_t` if one doesn't care about the actual size, I guess. Not sure how that would differ to `int_leastX_t`. But I agree that this is no duplicate. – Lukas-T Jul 15 '20 at 06:24
  • @churill Okay, that makes a bit of sense... for specific areas such as Graphics, Networking, etc. where having a fixed size is important, then the appropriate choice would be `intX_t`, however, if you aren't worried about fixed sizes or alignment and you are looking for as fast as possible computations that a particular architecture and compiler combo can handle, then `int_fastX_t` would be appropriate. – Francis Cugler Jul 15 '20 at 06:31
  • 1
    Do you need more than 32 bits to store each dimension? You may probably find more differences using those smaller types. – Bob__ Jul 15 '20 at 06:55
  • @Bob__ true... but regardless if they're `32` or `64` bits in size... the concept of the question still applies... – Francis Cugler Jul 15 '20 at 08:55

0 Answers0