Project

General

Profile

Bug #13354

Improve Time#<=> performance

Added by watson1978 (Shizuo Fujita) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-dev:50034]

Description

Time#<=> will be faster around 60%.
If internal values would have Fixnum,
optimized function improves performance.
(https://github.com/ruby/ruby/blob/9b69e9fafc329aaa540d5adeb55124f020abfe3c/time.c#L57-L67)

Before

       user     system      total        real
   1.410000   0.000000   1.410000 (  1.407848)

After

       user     system      total        real
   0.880000   0.000000   0.880000 (  0.886662)

Test code

require 'benchmark'

Benchmark.bmbm do |x|

  x.report do
    t1 = Time.now
    t2 = Time.now
    10000000.times do
      t1 <=> t2
    end
  end

end

Patch

The patch is in https://github.com/ruby/ruby/pull/1546

Associated revisions

Revision 92ea637c
Added by watson1978 (Shizuo Fujita) over 2 years ago

Improve Time#<=> performance

  • time.c (wcmp): use internal cmp() function for comparing internal Fixnum value
    in Time objects. On 64-bit machine, Time object might have Fixnum object
    internally by default and cmp() can compare the Fixnum objects directly.

    Time#<=> will be faster around 60% on 64-bit machine.

  • time.c (cmp): add optimized path for comparing internal Bignum value by using
    rb_big_cmp() API. On 32-bit machine, Time object might have Bignum object
    internally by default.

    Time#<=> will be faster around 50% on 32-bit machine.

    [ruby-dev:50034] [Bug #13354] [Fix GH-1546]

Before

         user     system      total        real

Fixnum 1.410000 0.000000 1.410000 ( 1.407848)
Bignum 1.550000 0.000000 1.550000 ( 1.549145)

After

         user     system      total        real

Fixnum 0.880000 0.000000 0.880000 ( 0.886662)
Bignum 1.050000 0.000000 1.050000 ( 1.047994)

Test code

require 'benchmark'

Benchmark.bmbm do |x|

x.report "Fixnum" do
t1 = Time.now
t2 = Time.now
10000000.times do
t1 <=> t2
end
end

x.report "Bignum" do
t1 = Time.at(2 ** 64)
t2 = Time.at(2 ** 64 + 1)
10000000.times do
t1 <=> t2
end
end

end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58828 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 58828
Added by watson1978 (Shizuo Fujita) over 2 years ago

Improve Time#<=> performance

  • time.c (wcmp): use internal cmp() function for comparing internal Fixnum value
    in Time objects. On 64-bit machine, Time object might have Fixnum object
    internally by default and cmp() can compare the Fixnum objects directly.

    Time#<=> will be faster around 60% on 64-bit machine.

  • time.c (cmp): add optimized path for comparing internal Bignum value by using
    rb_big_cmp() API. On 32-bit machine, Time object might have Bignum object
    internally by default.

    Time#<=> will be faster around 50% on 32-bit machine.

    [ruby-dev:50034] [Bug #13354] [Fix GH-1546]

Before

         user     system      total        real

Fixnum 1.410000 0.000000 1.410000 ( 1.407848)
Bignum 1.550000 0.000000 1.550000 ( 1.549145)

After

         user     system      total        real

Fixnum 0.880000 0.000000 0.880000 ( 0.886662)
Bignum 1.050000 0.000000 1.050000 ( 1.047994)

Test code

require 'benchmark'

Benchmark.bmbm do |x|

x.report "Fixnum" do
t1 = Time.now
t2 = Time.now
10000000.times do
t1 <=> t2
end
end

x.report "Bignum" do
t1 = Time.at(2 ** 64)
t2 = Time.at(2 ** 64 + 1)
10000000.times do
t1 <=> t2
end
end

end

Revision 58828
Added by watson1978 (Shizuo Fujita) over 2 years ago

Improve Time#<=> performance

  • time.c (wcmp): use internal cmp() function for comparing internal Fixnum value
    in Time objects. On 64-bit machine, Time object might have Fixnum object
    internally by default and cmp() can compare the Fixnum objects directly.

    Time#<=> will be faster around 60% on 64-bit machine.

  • time.c (cmp): add optimized path for comparing internal Bignum value by using
    rb_big_cmp() API. On 32-bit machine, Time object might have Bignum object
    internally by default.

    Time#<=> will be faster around 50% on 32-bit machine.

    [ruby-dev:50034] [Bug #13354] [Fix GH-1546]

Before

         user     system      total        real

Fixnum 1.410000 0.000000 1.410000 ( 1.407848)
Bignum 1.550000 0.000000 1.550000 ( 1.549145)

After

         user     system      total        real

Fixnum 0.880000 0.000000 0.880000 ( 0.886662)
Bignum 1.050000 0.000000 1.050000 ( 1.047994)

Test code

require 'benchmark'

Benchmark.bmbm do |x|

x.report "Fixnum" do
t1 = Time.now
t2 = Time.now
10000000.times do
t1 <=> t2
end
end

x.report "Bignum" do
t1 = Time.at(2 ** 64)
t2 = Time.at(2 ** 64 + 1)
10000000.times do
t1 <=> t2
end
end

end

Revision 58828
Added by watson1978 (Shizuo Fujita) over 2 years ago

Improve Time#<=> performance

  • time.c (wcmp): use internal cmp() function for comparing internal Fixnum value
    in Time objects. On 64-bit machine, Time object might have Fixnum object
    internally by default and cmp() can compare the Fixnum objects directly.

    Time#<=> will be faster around 60% on 64-bit machine.

  • time.c (cmp): add optimized path for comparing internal Bignum value by using
    rb_big_cmp() API. On 32-bit machine, Time object might have Bignum object
    internally by default.

    Time#<=> will be faster around 50% on 32-bit machine.

    [ruby-dev:50034] [Bug #13354] [Fix GH-1546]

Before

         user     system      total        real

Fixnum 1.410000 0.000000 1.410000 ( 1.407848)
Bignum 1.550000 0.000000 1.550000 ( 1.549145)

After

         user     system      total        real

Fixnum 0.880000 0.000000 0.880000 ( 0.886662)
Bignum 1.050000 0.000000 1.050000 ( 1.047994)

Test code

require 'benchmark'

Benchmark.bmbm do |x|

x.report "Fixnum" do
t1 = Time.now
t2 = Time.now
10000000.times do
t1 <=> t2
end
end

x.report "Bignum" do
t1 = Time.at(2 ** 64)
t2 = Time.at(2 ** 64 + 1)
10000000.times do
t1 <=> t2
end
end

end

History

#1

Updated by watson1978 (Shizuo Fujita) over 2 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r58828.


Improve Time#<=> performance

  • time.c (wcmp): use internal cmp() function for comparing internal Fixnum value
    in Time objects. On 64-bit machine, Time object might have Fixnum object
    internally by default and cmp() can compare the Fixnum objects directly.

    Time#<=> will be faster around 60% on 64-bit machine.

  • time.c (cmp): add optimized path for comparing internal Bignum value by using
    rb_big_cmp() API. On 32-bit machine, Time object might have Bignum object
    internally by default.

    Time#<=> will be faster around 50% on 32-bit machine.

    [ruby-dev:50034] [Bug #13354] [Fix GH-1546]

Before

         user     system      total        real

Fixnum 1.410000 0.000000 1.410000 ( 1.407848)
Bignum 1.550000 0.000000 1.550000 ( 1.549145)

After

         user     system      total        real

Fixnum 0.880000 0.000000 0.880000 ( 0.886662)
Bignum 1.050000 0.000000 1.050000 ( 1.047994)

Test code

require 'benchmark'

Benchmark.bmbm do |x|

x.report "Fixnum" do
t1 = Time.now
t2 = Time.now
10000000.times do
t1 <=> t2
end
end

x.report "Bignum" do
t1 = Time.at(2 ** 64)
t2 = Time.at(2 ** 64 + 1)
10000000.times do
t1 <=> t2
end
end

end

Also available in: Atom PDF