Skip to content

Instantly share code, notes, and snippets.

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@litch
litch / pubsub.rb
Last active August 29, 2015 13:57
require 'rubygems'
require 'ffi-rzmq'
require 'benchmark'
require 'json'
TESTS = 100_000
def error_check(rc)
if ZMQ::Util.resultcode_ok?(rc)
false

Keybase proof

I hereby claim:

  • I am litch on github.
  • I am litch (https://keybase.io/litch) on keybase.
  • I have a public key whose fingerprint is 2274 BE70 B287 7742 C4B4 1112 D88C E0B2 D7D4 D12A

To claim this, I am signing this object:

@litch
litch / postgres_experiment.mdown
Created December 11, 2013 17:32
Performance testing looking in arrays of uuids vs arrays of integers

I've added some functionality recently to be able to search a business by old UUID's, and am going to check the performance of it. So this little snippet will create a million rows in each of two tables and I'll post the results here as I get them (currently it's creating the rows, very slowly, probably because of the 4x uuid generation then indexing on the uuid column and the merged_uuids columns (and the indexes).

CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';


CREATE TABLE test_record_uuid (
    id uuid DEFAULT uuid_generate_v4() NOT NULL,
    name character varying(255),
=render @memory_page.approved_comments
= simple_form_for [@memory_page, Comment.new], html: {class: 'form-inline'} do |f|
.form-inputs
.row
.col-md-12
= f.input :comment, label: "Add Your Comment", input_html: {class: 'editable'}
.row#hidden_form_elements.hide //I have some JS callback that will display these fields once the parent input has been edited
.col-md-6
= f.input :name
.col-md-6
@litch
litch / irb.rb
Last active December 29, 2015 10:59
Ruby/Rails Time vs Datetime
☺ ~% irb
>> Time.now.to_s
=> "2013-11-26 09:33:52 -0600"
>> "2013-11-26 09:33:52 -0600".to_datetime
NoMethodError: undefined method `to_datetime' for "2013-11-26 09:33:52 -0600":String
from (irb):2
from /Users/litch/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
>> "2013-11-26 09:33:52 -0600".to_time
NoMethodError: undefined method `to_time' for "2013-11-26 09:33:52 -0600":String
from (irb):3
@litch
litch / module_class_instance_method_comparison.rb
Created November 3, 2013 03:43
Slightly surprisingly small amount of performance difference for these three methods of getting things done...
require 'redis'
require 'benchmark'
module ModuleMethods
def self.record_metric(name)
$REDIS.lpush(translate_name(name), Time.now.to_f)
end
def self.translate_name(queue)
@litch
litch / gist:6543765
Last active December 22, 2015 22:59
Storing Regex in a constant instead of a method saves a lot of memory, but not a lot of time.
?> def get_memory_usage
>> `ps -o rss= -p #{Process.pid}`.to_i
>> end
=> nil
>>
?> before = get_memory_usage
=> 20296
>>
?> n.times {MethodRegexObject.new}
@litch
litch / percentile_finder.rb
Created August 8, 2013 18:01
MongoDB Logger Request timing percentile finder
class PercentileFinder
attr_accessor :action, :controller, :limit, :calls, :dataset
def initialize(action, controller, limit = 1000)
self.action = action
self.controller = controller
self.limit = limit
end
def find(percentile_looking_for = 0.95)
@litch
litch / rpn_3_ways.rb
Last active December 20, 2015 01:29
RPN 3 ways. I had two - then tried to do the pattern-matching solution, wound up being intermediate performance...
require 'rspec/autorun'
require 'benchmark'
describe "polish notation" do
it "do a trivial case" do
polish_parser(:+, 1, 1).first.should == 2
reverse_stack_polish_parser(:+, 1, 1).first.should == 2
pm_polish_parser(:+, 1, 1).first.should == 2
end