开发者

Why does some Ruby code run twice as fast on a 2.53GHz than on a 2.2GHz Core 2 Duo processor?

开发者 https://www.devze.com 2023-01-23 02:49 出处:网络
(This question attempts to find out why the running of a program can be different on different processors, so it is related to the performance aspect of programming.)

(This question attempts to find out why the running of a program can be different on different processors, so it is related to the performance aspect of programming.)

The following program will take 3.6 seconds to run on a Macbook that has 2.2GHz Core 2 Duo, and 1.8 seconds to run on a Macbook Pro that has 2.53GHz Core 2 Duo. Why is that?

That's a bit weird... why doubling the speed when the CPU is only 15% faster in clock speed? I double checked the CPU meter to make sure none of the 2 cores are in 100% usage (so as to see the CPU is not busy running something else). Could it be because one is Mac OS X Leopard and one is Mac OS X Snow Leopard 开发者_如何学编程(64 bit)? Both are running Ruby 1.9.2.

p RUBY_VERSION
p RUBY_DESCRIPTION if defined? RUBY_DESCRIPTION
n = 9_999_999
p n

t = 0; 1.upto(n) {|i| t += i if i%3==0 || i%5==0}; p t

The following are just output of the program:

On 2.2GHz Core 2 Duo: (Update: Macbook identifier: MacBook3,1, therefore probably is Intel Core 2 Duo (T7300/T7500))

$ time ruby 1.rb
"1.9.2"
"ruby 1.9.2p0 (2010-08-18 revision 29036) [i386-darwin9.8.0]"
9999999
23333331666668

real  0m3.784s
user  0m3.751s
sys  0m0.021s

2.53GHz Intel Core 2 Duo: (Update: Macbook identifier: MacBookPro5,4, therefore probably is Intel Core 2 Duo Penryn with 3 MB on-chip L2 cache)

$ time ruby 1.rb
"1.9.2"
"ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]"
9999999
23333331666668

real  0m1.893s
user  0m1.809s
sys  0m0.012s

Test run on Windows 7:

time_start = Time.now

p RUBY_VERSION
p RUBY_DESCRIPTION if defined? RUBY_DESCRIPTION

n = 9_999_999
p n

t = 0; 1.upto(n) {|i| t += i if i%3==0 || i%5==0}; p t

print "Took #{Time.now - time_start} seconds to run\n"

Intel Q6600 Quad Core 2.4GHz running Windows 7, 64-bit:

C:\> ruby try.rb
"1.9.2"
"ruby 1.9.2p0 (2010-08-18) [i386-mingw32]"
9999999
23333331666668
Took 3.248186 seconds to run

Intel 920 i7 2.67GHz running Windows 7, 64-bit:

C:\> ruby try.rb
"1.9.2"
"ruby 1.9.2p0 (2010-08-18) [i386-mingw32]"
9999999
23333331666668
Took 2.044117 seconds to run

It is also strange why an i7 with 2.67GHz is slower than a 2.53GHz Core 2 Duo.


I suspect that ruby is switching to a arbitrary-precision integer implementation later on the 64 bit os.

Quoting the Fixnum ruby doc:

A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). If any operation on a Fixnum exceeds this range, the value is automatically converted to a Bignum.

Here, a native machine word is technically 64 bit, but the interpreter is compiled to run on 32 bit processors.


why doubling the speed when the CPU is only 15% faster in clock speed?

Quite simply because the performance of computers is determined not solely by CPU clock speed.

Other things to consider are:

  • CPU architectures, including e.g. the number of cores on a CPU, or the general ability to run multiple instructions in parallel
  • other clock speeds in the system (memory, FSB)
  • CPU cache sizes
  • installed memory chips (some are faster than others)
  • additionally installed hardware (might slow down the system through hardware interruptions)
  • different operating systems
  • 32-bit vs. 64-bit systems

I'm sure there's a lot more things to add to the above list. I won't elaborate further on the point, but if anyone feels like it, please, feel free to add to the above list.


In out CI environment we have a lot of "pizza box" computers that are supposed to be identical. They have the same hardware, were installed at the same time, and should be generally identical. They're even placed in "thermally equivalent" locations. They're not identical, and the variation can be quite stunning.

The only conclusion I have come up with is different binnings of CPU will have different thresholds for thermal stepping; some of the "best" chips hold up better. I also suspect other "minor" hardware faults/variations to be playing a role here. Maybe the slow boxes have slightly different components that play less well together ?

There are tools out there that will show you if your CPU is throttling for thermal reasons.


I don't know much Ruby, but your code doesn't look to be multithreaded, if that's the case it's not going to take advantage of multiple cores. There can also be large differences between two CPU models. You have smaller process sizes, larger caches, better SIMD instructions sets, faster memory access, etc... Compiler & OS differences can cause large swings in performance between Windows & Linux, this can also be said for x86 vs x64. Plus Core i7s support HyperThreading which in some cases makes a single threaded app slower.

Just as an example, if that 2.2Ghz CPU is an Intel Core2 E4500 it has the following specs:

  • Clock: 2.2Ghz
  • L2 Cache: 2MB
  • FSB: 800MT/sec
  • Process Size: 65nm

vs a T9400 which is likely in your MacBook Pro

  • Clock: 2.53Ghz
  • L2 Cache: 6MB
  • FSB: 1066MT/sec
  • Process Size: 45nm

Plus you're running it on an x64 build of Darwin. All those things could definitely add up to inflating a trivial little script into executing much faster.


didn't read the code, But.. It is really hard to test on 2 different computers. You need exactly the same os, same processes, same amount of memory.

If you change the processor family (i7, core2due, P4, P4-D) - the processor frequency says nothing on each processor abilities against another family. you can only compare in the same family (a newer processor might invest cycles in core management rather in computation for example)

0

精彩评论

暂无评论...
验证码 换一张
取 消