Skip to content

Instantly share code, notes, and snippets.

View huacnlee's full-sized avatar

Jason Lee huacnlee

View GitHub Profile
@huacnlee
huacnlee / gist:9525727
Created March 13, 2014 10:20
Ruby Array include? vs index
require 'benchmark'
total_times = 30000
items1 = []
items2 = []
items3 = []
Benchmark.bm do |x|
x.report("include?") {
total_times.times do |i|
@huacnlee
huacnlee / gist:9907235
Created April 1, 2014 03:35
Benchmark for redis-search creating indexes by 0.9.6 VS previous versions
## Before
user system total real
Index 1,000 items 2.160000 0.820000 2.980000 ( 4.633564)
## After (0.9.6)
user system total real
Index 1,000 items 0.780000 0.070000 0.850000 ( 0.851309)
def api_request(action, params = {})
Rails.logger.info "Started request #{host_root} action: #{action}"
# Rails.logger.info " Params: #{params.inspect}"
t1 = Time.now
n = 0
begin
res = Faraday.post do |req|
req.url "http://api.kanbox.com"
req.body = Kanbox::Param.parse(action, params)
req.options[:open_timeout] = 30
@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
@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 / 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 / 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 / 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 / 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 / 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'