Skip to content

Instantly share code, notes, and snippets.

View huacnlee's full-sized avatar

Jason Lee huacnlee

View GitHub Profile
@huacnlee
huacnlee / benchmark-results.txt
Created December 16, 2015 03:33
GitLab Ability class add class method cache benchmarks
Calculating -------------------------------------
project_guest_rules without method cache
79.352k i/100ms
project_guest_rules with method cache
93.634k i/100ms
-------------------------------------------------
project_guest_rules without method cache
2.865M (±32.5%) i/s - 11.982M
project_guest_rules with method cache
4.419M (± 7.4%) i/s - 22.004M
@huacnlee
huacnlee / 0_custom_extends.rb
Last active November 18, 2015 02:51
Extend Rails Application without change original files
# config/initializes/0_custom_extends.rb
# 让 Rails Autoload Path 载入 lib/custom 里面的内容
# 让 lib/custom/uploaders 里面的东西优先级更高
ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/uploaders")
ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/models")
ActiveSupport::Dependencies.autoload_paths += %W(
#{Rails.root}/lib/custom/models
#{Rails.root}/lib/custom/models/hooks
#{Rails.root}/lib/custom/models/concerns
#{Rails.root}/app/models
@huacnlee
huacnlee / alias_method_chain_test.rb
Created October 22, 2015 03:55
A simple case for use ActiveSupport alias_method_chain
require 'active_support/all'
class Foo
def foo
bar
puts "foo"
end
private
def bar
@huacnlee
huacnlee / test_railtie.rb
Last active August 29, 2015 14:21
Benchmark sprockets-rails javascript_include_tag cache
# sprockets-rails/test/test_railtie.rb
def test_benchmark_assets_tag
app.configure do
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["bar.js", "bar.css"]
end
app.initialize!
require 'benchmark/ips'
@huacnlee
huacnlee / libeasy_homebrew.rb
Last active August 29, 2015 14:18
libeasy_homebrew.rb
class Libeasy < Formula
homepage "http://gitlab.alibaba-inc.com/zicheng.lhs/libeasy"
head "http://gitlab.alibaba-inc.com/zicheng.lhs/libeasy.git"
version "1.0.3"
depends_on "libtool" => :build
depends_on "automake" => :build
depends_on "autoconf" => :build
def install
@huacnlee
huacnlee / DMSToDecimal.java
Created November 17, 2014 11:12
用于将 GPS 的坐标格式转成浮点数, 如 DMSToDecimal.convert("N 30d17m27.786712646484375s") => 30.282034
import javax.xml.bind.DatatypeConverter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// 用于将 GPS 的坐标格式转成浮点数
// 如 DMSToDecimal.convert("N 30d17m27.786712646484375s") => 30.282034
public class DMSToDecimal {
private static final String REGEXP = "(N|E|W|S)([\\.\\d\\s]+)d([\\.\\d\\s]+)m([\\.\\d\\s]+)s";
private static double dmsToDecimal(String hemisphereOUmeridien,double degres,double minutes,double secondes){
@huacnlee
huacnlee / asset_path_after.rb
Last active August 29, 2015 14:07
Benchmark for ActiveView asset_path helper
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}i
def asset_path(source, options = {})
source = source.to_s
return "" unless source.present?
return source if source =~ URI_REGEXP
tail = source[/([\?#].+)$/] || ''
source.chomp!(tail)
if extname = compute_asset_extname(source, options)
@huacnlee
huacnlee / stirng_strart_with_vs_first_char.rb
Created October 8, 2014 13:42
String start_with? vs [0] ==
Calculating -------------------------------------
str.start_with? 78491 i/100ms
str[0] == '/' 66410 i/100ms
-------------------------------------------------
str.start_with? 3663514.9 (±7.8%) i/s - 18209912 in 5.002869s
str[0] == '/' 2905683.6 (±7.6%) i/s - 14477380 in 5.011908s
Comparison:
str.start_with?: 3663514.9 i/s
str[0] == '/': 2905683.6 i/s - 1.26x slower
@huacnlee
huacnlee / freeze_benchmark.rb
Created September 30, 2014 07:52
String freeze vs no freeze
task :test_string_freeze_vs_nofreeze do
str = "this is a example string"
Benchmark.ips do |x|
x.report("no_freeze") {
str.index("example")
}
x.report("freeze") {
str.index("example".freeze)
}
@huacnlee
huacnlee / keys_check_benchmark.rb
Created September 30, 2014 07:48
Ruby hash.keys.index vs hash.has_key?
Calculating -------------------------------------
keys.index 1794 i/100ms
has_key? 1822 i/100ms
-------------------------------------------------
keys.index 4036912.9 (±10.4%) i/s - 8691930 in 2.275054s
has_key? 6605458.9 (±9.1%) i/s - 25340376 in 3.878441s
Comparison:
has_key?: 6605458.9 i/s
keys.index: 4036912.9 i/s - 1.64x slower