-6

Would it be char, byte, Int16, Int32, Int64 (maybe last three unsigned, since I wouldn't have negative numbers?).

I need it for multiplication and adding. The smaller numbers can contain a type, the more parts a big number will be divided into.

An example: 1234567898765321
In char: {1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1}
In Int32: {123456789, 87654321}

So, which is faster to use for billions of calculations?

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Randolph
  • 57
  • 4
  • An array reference variable is still a variable. It doesn't matter. – Edward J Beckett Nov 19 '12 at 10:04
  • But I can have more calculations if using char type (maybe faster) or having less calculations using int32 type (can hold bigger numbers, maybe faster) – Randolph Nov 19 '12 at 10:07
  • Your missing the point. The array itself is not have any bearing on branch prediction. – Edward J Beckett Nov 19 '12 at 10:09
  • Randolph ... please see [Processing a Sorted Array](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array) for more info. – Edward J Beckett Nov 19 '12 at 10:12
  • But I have to do it this way. Is there any other "normal" way to add to 1 million-digit numbers? I don't think so. Then I have to use arrays. Numerical, for the best performance. I can split these great numbers into smaller (one element - one digit) or a bit bigger (one element - 4 digits). Is there a difference? – Randolph Nov 19 '12 at 10:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19750/discussion-between-eddie-b-and-randolph) – Edward J Beckett Nov 19 '12 at 10:14

1 Answers1

3

If you mean:

Can I get a speed advantage by splitting large numbers into small pieces and doing my own carrying logic for addition and multiplication?

The answer is no. Use types that are large enough to hold the entire value, and the compiler/JIT will generate machine code that does each arithmetic operation in a single instruction, which will be as fast as possible.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • It's exactly what I mean. But what if I have a number that has 100.000 digits? I must have some splits. – Randolph Nov 19 '12 at 10:02
  • @Randolph: Then use `BigInteger`. – Jon Skeet Nov 19 '12 at 10:04
  • Unfortunately, I cant. I have to write my own arithmetic without it. – Randolph Nov 19 '12 at 10:05
  • @Randolph - as an academic exercise? Then you should do your homework yourself and find out which is fastest! :p – Daniel Earwicker Nov 19 '12 at 10:07
  • Not an academic, rather a theory that would be used for everything I'll be doing for the academic exercises :) I did some benchmark. From it, Int32 was faster than Int64 and both faster than char. But to be sure, I would have to write different functions, each having other idea. So before it, I just wanted to ask. – Randolph Nov 19 '12 at 10:09
  • Did you have any theories for why Int32 was faster than Int64? What hardware were you running on? – Daniel Earwicker Nov 19 '12 at 10:11
  • Another option: http://stackoverflow.com/questions/5894696/how-to-use-gpu-for-mathematics – Daniel Earwicker Nov 19 '12 at 10:12
  • Intel i7, 2.93 GHz. It just was faster :) If I have, let's say, a two numbers 12345678987654321, it was faster to add 1234567 + 1234567 and 8987654321 + 8987654321 than these two numbers right away. – Randolph Nov 19 '12 at 10:16
  • @Randolph computers are designed to understand two numbers; zero and one ... learn up about binary math and this _might_ become clearer. – Edward J Beckett Nov 19 '12 at 10:24