Issue #18293 has been updated by k0kubun (Takashi Kokubun).

File patch.diff added

`Time.at` has complicated default arguments. I believe that would result in 5 local variable assignments in VM and be the bottleneck.

If you apply the attached patch and use this benchmark script:

```yml
benchmark:
  - Time.at(0)
  - Time.at_c(0)
  - Time.at_1arg(0)
```

the result is:

```
$ benchmark-driver -v /tmp/a.yml --rbenv=ruby
ruby: ruby 3.1.0dev (2021-11-10T01:08:48Z master af74cc7504) [x86_64-linux]
Warming up --------------------------------------
          Time.at(0)     6.141M i/s -      6.164M times in 1.003748s (162.85ns/i, 618clocks/i)
        Time.at_c(0)     9.847M i/s -      9.945M times in 1.009962s (101.56ns/i, 385clocks/i)
     Time.at_1arg(0)     8.976M i/s -      9.089M times in 1.012540s (111.40ns/i, 423clocks/i)
Calculating -------------------------------------
          Time.at(0)     6.691M i/s -     18.422M times in 2.753387s (149.47ns/i, 567clocks/i)
        Time.at_c(0)    11.616M i/s -     29.541M times in 2.543150s (86.09ns/i, 327clocks/i)
     Time.at_1arg(0)    10.710M i/s -     26.929M times in 2.514397s (93.37ns/i, 354clocks/i)

Comparison:
        Time.at_c(0):  11615765.0 i/s
     Time.at_1arg(0):  10709979.3 i/s - 1.08x  slower
          Time.at(0):   6690511.4 i/s - 1.74x  slower
```

So the bottleneck seems to be not the `Primitive.` call but the complicated arguments.

----------------------------------------
Bug #18293: Time.at in master branch was 25% slower then Ruby 3.0
https://bugs.ruby-lang.org/issues/18293#change-94541

* Author: watson1978 (Shizuo Fujita)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.0dev (2021-11-08T13:15:21Z master bd2674ad33) [arm64-darwin21]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
       | Ruby 3.0.2 | Ruby 3.1.0-dev | result
--       | --         | --             | --
Time.at  | 8.223M     | 6.190M         | 0.753x

### Environment
- MacBook Pro (14inch, 2021)
- macOS 12.0
- Apple M1 Max
- Apple clang version 13.0.0 (clang-1300.0.29.3)

### Ruby 3.1.0-dev
```
$ ruby -v time.rb
ruby 3.1.0dev (2021-11-08T13:15:21Z master bd2674ad33) [arm64-darwin21]
Warming up --------------------------------------
             Time.at   614.843k i/100ms
Calculating -------------------------------------
             Time.at      6.190M ( 0.3%) i/s -     31.357M in   5.065559s
```

### Ruby 3.0.2
```
$ ruby -v time.rb
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin21]
Warming up --------------------------------------
             Time.at   821.722k i/100ms
Calculating -------------------------------------
             Time.at      8.223M ( 0.6%) i/s -     41.908M in   5.096820s
```

### Test code
```ruby
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report('Time.at') { Time.at(0) }
end
```

---Files--------------------------------
patch.diff (2.52 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>