Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does i32 perform better than i64 using Rust on Win8-64?

Tags:

rust

I've recently started looking at Rust. I'm of the view that for application programs, I would prefer to use just two number-types (i64 and f64 - in general), because I think that anything that my code may run on will be 64-bit, and I believe that every line of code is important, because brevity is important, and I don't want to constantly convert numbers from eg. i32/int to i64, etc.

Using Rust 0.8 on Win8-64: (IE: rust 0.8 host: i686-pc-mingw32).

I wrote a minor test-program to see what the performance difference is. Given the timings that resulted, I wanted to determine why there is the difference that exists. According to the literature that I looked at, Rust will use the integer type used by the processor for addressing memory. As far as I know, on a 64-bit processor (Intel Core-i5), that is 64 bits. It would however appear that "int" on Rust is using 32-bit, and also that 32-bit appears faster than 64-bit. While I think that the difference in performance (given the number of iterations in the test) is relatively insignificant for an application-program, it still raises questions as indicated. IE: Is 32-bit int used for 64-bit processors, and why is 32-bit faster on a 64-bit machine?

The results of my test program are as follows. The times indicated are consistent over many repeats:

1000000000 iters (i64 iters and i64 simple addition) .....
Total Elapsed time = 3237.86 ms
Result from i64 addition = 2000000000

1000000000 iters (int iters and int simple addition) .....
Total Elapsed time = 2353.45 ms
Result from int addition = 2000000000

1000000000 iters (i32 iters and i32 simple addition) .....
Total Elapsed time = 2375.52 ms
Result from i32 addition = 2000000000

The code used is as follows:

fn fTesti64Add(mut iIterMax:i64) -> i64 {
  println(fmt!("\n%s iters (i64 iters and i64 simple addition) .....",
               iIterMax.to_str()));
  let uTmeStart:u64 = extra::time::precise_time_ns();
  let mut iTot:i64 = 0;
  while iIterMax>0 {
     iTot += 2;
     iIterMax -=1;
  }
  fDisplayElapsed(uTmeStart, extra::time::precise_time_ns());
  return iTot;
} 

fn fTestIntAdd (mut iIterMax:int) -> int {
 println(fmt!("\n%d iters (int iters and int simple addition) .....", iIterMax));
 let uTmeStart:u64 = extra::time::precise_time_ns();
  let mut iVal:int = 0;
  while iIterMax > 0 {
     iVal += 2;
     iIterMax -= 1;
  }
  fDisplayElapsed(uTmeStart, extra::time::precise_time_ns());
  return iVal;
}

fn fTestI32Add (mut iIterMax:i32) -> i32 {
  println(fmt!("\n%s iters (i32 iters and i32 simple addition) .....",
               iIterMax.to_str()));
  let uTmeStart:u64 = extra::time::precise_time_ns();
  let mut iVal:i32 = 0;
  while iIterMax > 0 {
     iVal += 2;
     iIterMax -= 1;
  }
  fDisplayElapsed(uTmeStart, extra::time::precise_time_ns());
  return iVal;
}
like image 793
Brian Oh Avatar asked Oct 26 '13 02:10

Brian Oh


1 Answers

The answer is in: rust 0.8 host: i686-pc-mingw32

i686 is the 32 bit instruction set(Aka x86), so you are running the 32bit version of rust on your 64 bit cpu. And the 32 bit software can't use the 64 bit integer math.

So you need a 64 bit rust.

like image 193
MTilsted Avatar answered Oct 27 '22 06:10

MTilsted