如何将 CFLAGS 添加到 RVM?
How to add CFLAGS to RVM?
我要编译ruby-2.7.1。但是我不想覆盖系统的 ruby 2.7.1 因为很多包都依赖于 Ruby.
所以我最简单的选择是RVM。但我想在本地编译 Ruby 因为我永远不需要将二进制文件复制到另一个系统。有没有办法将 -O3
、-march=native
和 -mtune=native
选项附加到 CFLAGS?
所以我最终使用了:
CC=gcc HOSTCC=gcc CFLAGS="-O3 -pipe -fno-plt -march=native -mtune=native" CXXFLAGS="-O3 -pipe -fno-plt -march=native -mtune=native" .rvm/bin/rvm install ruby-2.7.1
检查(编译后):
~/.rvm/rubies/ruby-2.7.1/bin/ruby -e "puts RbConfig::CONFIG.then { |x| [x['CFLAGS'], x['CXXFLAGS']] }"
输出应该与此类似:
-O3 -pipe -fno-plt -march=native -mtune=native -fPIC
-O3 -pipe -fno-plt -march=native -mtune=native
我做了一个基准测试并获得了微小的性能提升:
def prime(range)
(numbers = (2..range).to_a.unshift(nil, nil)).each do |num|
next if num.nil?
return numbers.compact if (sqr = num ** 2) > range
sqr.step(range, num) { |x| numbers[x] = nil }
end
end
def pi(n)
q, r, t, k, m, x, str = 1, 0, 1, 1, 3, 3, ''
if 4 * q + r - t < m * t
str.concat(m.to_s)
q, r, m = 10 * q, 10 * (r - m * t), (10 * (3 * q + r)) / t - 10 * m
else
q, r, t, k, m, x = q * k, (2 * q + r) * x, t * x, k + 1, (q * (7 * k + 2) + r * x) / (t * x), x + 2
end while str.length < n
str[1, 0] = '.'
str
end
puts "Calculating prime numbers"
total_time = 0
10.times do
time = Time.now
prime(5_000_000)
t = Time.now - time
puts "Time taken: #{t} s"
total_time += t
end
puts "Total time: #{total_time}\nCalculating digits of pi"
total_time = 0
5.times do
time = Time.now
pi(3000)
t = Time.now - time
puts "Time taken: #{t} s"
total_time += t
end
puts "Total time: #{total_time}"
输出:
- Ruby 2.7.1-3(通用 x64)(由 Archlinux 社区编译):
Calculating prime numbers
Time taken: 0.875989347 s
Time taken: 0.867194701 s
Time taken: 0.888623001 s
Time taken: 0.896203257 s
Time taken: 0.878371141 s
Time taken: 0.870071403 s
Time taken: 0.885831722 s
Time taken: 0.893377101 s
Time taken: 0.878395115 s
Time taken: 0.869960278 s
Total time: 8.804017066
Calculating digits of pi
Time taken: 0.789893805 s
Time taken: 0.77520548 s
Time taken: 0.775608948 s
Time taken: 0.775225198 s
Time taken: 0.774519429 s
Total time: 3.8904528600000003
- Ruby 2.7.1(本机,x64)(现在编译):
Calculating prime numbers
Time taken: 0.784012172 s
Time taken: 0.77873184 s
Time taken: 0.794907106 s
Time taken: 0.804767279 s
Time taken: 0.784964685 s
Time taken: 0.779715785 s
Time taken: 0.795503235 s
Time taken: 0.803209593 s
Time taken: 0.785618289 s
Time taken: 0.77906647 s
Total time: 7.890496454000001
Calculating digits of pi
Time taken: 0.794860247 s
Time taken: 0.768611289 s
Time taken: 0.77029717 s
Time taken: 0.753028667 s
Time taken: 0.752961357 s
Total time: 3.83975873
在基准测试期间,我使用了相同的终端,没有 GUI 进程 运行ning。我以 4 - 5 秒的间隔进行了基准测试。即使我 运行 一次又一次地测试,我也可以确认相同的输出。
我要编译ruby-2.7.1。但是我不想覆盖系统的 ruby 2.7.1 因为很多包都依赖于 Ruby.
所以我最简单的选择是RVM。但我想在本地编译 Ruby 因为我永远不需要将二进制文件复制到另一个系统。有没有办法将 -O3
、-march=native
和 -mtune=native
选项附加到 CFLAGS?
所以我最终使用了:
CC=gcc HOSTCC=gcc CFLAGS="-O3 -pipe -fno-plt -march=native -mtune=native" CXXFLAGS="-O3 -pipe -fno-plt -march=native -mtune=native" .rvm/bin/rvm install ruby-2.7.1
检查(编译后):
~/.rvm/rubies/ruby-2.7.1/bin/ruby -e "puts RbConfig::CONFIG.then { |x| [x['CFLAGS'], x['CXXFLAGS']] }"
输出应该与此类似:
-O3 -pipe -fno-plt -march=native -mtune=native -fPIC
-O3 -pipe -fno-plt -march=native -mtune=native
我做了一个基准测试并获得了微小的性能提升:
def prime(range)
(numbers = (2..range).to_a.unshift(nil, nil)).each do |num|
next if num.nil?
return numbers.compact if (sqr = num ** 2) > range
sqr.step(range, num) { |x| numbers[x] = nil }
end
end
def pi(n)
q, r, t, k, m, x, str = 1, 0, 1, 1, 3, 3, ''
if 4 * q + r - t < m * t
str.concat(m.to_s)
q, r, m = 10 * q, 10 * (r - m * t), (10 * (3 * q + r)) / t - 10 * m
else
q, r, t, k, m, x = q * k, (2 * q + r) * x, t * x, k + 1, (q * (7 * k + 2) + r * x) / (t * x), x + 2
end while str.length < n
str[1, 0] = '.'
str
end
puts "Calculating prime numbers"
total_time = 0
10.times do
time = Time.now
prime(5_000_000)
t = Time.now - time
puts "Time taken: #{t} s"
total_time += t
end
puts "Total time: #{total_time}\nCalculating digits of pi"
total_time = 0
5.times do
time = Time.now
pi(3000)
t = Time.now - time
puts "Time taken: #{t} s"
total_time += t
end
puts "Total time: #{total_time}"
输出:
- Ruby 2.7.1-3(通用 x64)(由 Archlinux 社区编译):
Calculating prime numbers
Time taken: 0.875989347 s
Time taken: 0.867194701 s
Time taken: 0.888623001 s
Time taken: 0.896203257 s
Time taken: 0.878371141 s
Time taken: 0.870071403 s
Time taken: 0.885831722 s
Time taken: 0.893377101 s
Time taken: 0.878395115 s
Time taken: 0.869960278 s
Total time: 8.804017066
Calculating digits of pi
Time taken: 0.789893805 s
Time taken: 0.77520548 s
Time taken: 0.775608948 s
Time taken: 0.775225198 s
Time taken: 0.774519429 s
Total time: 3.8904528600000003
- Ruby 2.7.1(本机,x64)(现在编译):
Calculating prime numbers
Time taken: 0.784012172 s
Time taken: 0.77873184 s
Time taken: 0.794907106 s
Time taken: 0.804767279 s
Time taken: 0.784964685 s
Time taken: 0.779715785 s
Time taken: 0.795503235 s
Time taken: 0.803209593 s
Time taken: 0.785618289 s
Time taken: 0.77906647 s
Total time: 7.890496454000001
Calculating digits of pi
Time taken: 0.794860247 s
Time taken: 0.768611289 s
Time taken: 0.77029717 s
Time taken: 0.753028667 s
Time taken: 0.752961357 s
Total time: 3.83975873
在基准测试期间,我使用了相同的终端,没有 GUI 进程 运行ning。我以 4 - 5 秒的间隔进行了基准测试。即使我 运行 一次又一次地测试,我也可以确认相同的输出。