Skip to content

Instantly share code, notes, and snippets.

View mykoweb's full-sized avatar

Mike Kim mykoweb

  • Clearbit
  • San Francisco, CA
View GitHub Profile
// This version of partition uses as media-of-three pivot
// instead of a randomized pivot.
func partition(data Interface, l, r int) int {
m := l + (r-l)/2 // Avoid integer overflow
medianOfThree(data, l, m, r) // Pivot is placed at l
i := l + 1
for j := l + 1; j <= r; j++ {
if data.Less(j, l) {
if i != j {
// This version of QuickSort reverts to InsertionSort
// when the slice size is below a cutoff value of 12.
// Add ShellSort before InsertionSort to help InsertionSort.
func QuickSort(data Interface, l int, r int, enableShellSort bool) {
if l >= r {
return
}
if r-l > 12 {
pivot := partition(data, l, r)
// This version of QuickSort reverts to InsertionSort when the
// slice size is below a certain cutoff value. The cutoff value
// is parametrizable.
func QuickSort(data Interface, l int, r int, cutoff int) {
if l >= r {
return
}
if r-l > cutoff {
pivot := partition(data, l, r)
package qsort
import "math/rand"
func QuickSort(data Interface, l int, r int) {
if l >= r {
return
}
pivot := partition(data, l, r)
# It's a good idea to disable refresh when indexing documents to improve performance
client.indices.put_settings index: 'PatientA', body: { refresh_interval: -1 }
client.indices.put_settings index: 'PatientB', body: { refresh_interval: -1 }
# Now index (store) the documents
patient_a_doc1 = {
patient_name: 'PatientA',
content: 'The quick brown fox'
}
patient_a_doc2 = {
req_body_for_patient_a = {
routing: 'PatientA',
filter: {
term: { patient_name: 'PatientA' }
}
}
req_body_for_patient_b = {
routing: 'PatientB',
filter: {
def raw_put(path, body)
conn = Faraday.new url: 'http://localhost:9200'
conn.put(path) do |req|
req.body = body.to_json
req.headers['Content-Type'] = 'application/json'
end
end
# Create primary index
req_body = {
require 'json'
require 'elasticsearch'
client = Elasticsearch::Client.new hosts: 'http://localhost:9200'
# Test that Elasticsearch is up
client.info
require 'faraday'
require 'benchmark'
@conn = Faraday.new(url: 'https://www.google.com')
@threads = []
Benchmark.bm(14) do |x|
x.report('no-threads') do
8.times { @conn.get }
end
require 'benchmark'
@threads = []
Benchmark.bm(14) do |x|
x.report('no-threads') do
8.times do
tmp_array = []
10_000_000.times { |n| tmp_array << n }
end
end