Skip to content

Instantly share code, notes, and snippets.

@c910335
c910335 / intro_sort.cr
Last active November 6, 2016 15:45
I have no idea why this is faster than std::sort in cpp.
class Array
def sort!
Array.intro_sort!(@buffer, @size)
self
end
def sort!(&block : T, T -> Int32)
Array.intro_sort!(@buffer, @size, block)
self
end
@c910335
c910335 / result
Created November 7, 2016 10:01
some sort benchmarks
# Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
# Arch Linux x86_64
# g++ (GCC) 6.2.1 20160830
# Crystal 0.19.4 (2016-10-07)
256 elements with original quick sort 702.73k (± 3.72%) 1.50× slower
256 elements with intro sort 1.05M (± 4.05%) fastest
256 elements with std::sort in cpp 845.94k (± 4.23%) 1.25× slower
256 elements with qsort in c 307.25k (± 5.24%) 3.43× slower
512 elements with original quick sort 320.05k (± 4.60%) 1.51× slower
512 elements with intro sort 483.13k (± 2.96%) fastest
@c910335
c910335 / result
Last active November 8, 2016 11:40 — forked from kostya/result
some sort benchmarks
# Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
# Arch Linux x86_64
# g++ (GCC) 6.2.1 20160830
# Crystal 0.19.4 (2016-10-07)
======== random 2097152 =============
(0.333) 2097152 elements with original quick sort
(0.247) 2097152 elements with intro sort
(0.245) 2097152 elements with std::sort in cpp
(0.444) 2097152 elements with qsort in c
# Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
# Arch Linux x86_64
# g++ (GCC) 6.2.1 20160830
# Crystal 0.19.4 (2016-10-07)
======== random 2097152 =============
with original quick sort 6.73 (± 2.34%) 1.16× slower
with intro sort 7.84 (± 1.49%) fastest
with std::sort in cpp 7.83 (± 1.74%) 1.00× slower
with qsort in c 4.98 (± 1.75%) 1.57× slower
======== seq 2097152 =============
@c910335
c910335 / stable_sort.cr
Created November 19, 2016 19:58
Stable sort in Crystal
class Array
def stable_sort
each_with_index.to_a.sort.map { |x| x[0] }
end
end
@c910335
c910335 / TestController.cr
Last active August 4, 2017 11:23
Amber before action issue
class TestController < ApplicationController
before_action do
all do
puts count
end
end
def count
@count ||= 0
@c910335
c910335 / input
Last active December 4, 2017 15:46
程式作業三開放測資
10
2
2 3
2 5
3
100 101
10 102
10 103
2217
561 25785
@c910335
c910335 / input
Last active December 20, 2017 14:26
程式作業四開放測資
3 3
0 1 2
1 2 3
0 2 4
8 12
0 1 3
0 2 1
1 2 1
1 4 5
1 5 4
@c910335
c910335 / answers.json
Last active March 10, 2018 13:46
SITCON 知識王
{
"資安趨勢部落格在文章中指出「伺服器變挖礦機將是未來日漸嚴重的問題」,並以 JenkinsMiner 為例,含有漏洞的伺服器一旦被 JenkinsMiner 入侵且成功部屬挖礦程式,將會造成 ___________ 影響,請問 _________ 為?":"效能嚴重變慢",
"趨勢科技在〈典範轉移〉一文中提出,BEC 詐騙已開始放棄使用鍵盤側錄程式,而改用 ____________ 進行。":"網路釣魚 PDF 附件",
"請問下列何者不為剖析器?":"LSR",
"chroot 是在 UNIX 系統的一個操作,針對正在運作的軟體行程和它的子行程,改變它外顯的根目錄。chroot 程式可歸類為何種資安相關的應用?":"Sandbox",
"下列何者是 Ubuntu 的套件管理程式?":"APT",
"可以用哪個程式查詢從本地到另一個 IP Address 所經過的 router?":"traceroute",
"140.128.77.10/25 這個網段有多少可用的 IP?":"126",
"在二元樹走訪的方法中,我們常聽到 LVR、LRV、VLR 三種方法,請問 V 代表?":"Visiting,對當前的 node 進行print、assign或其他操作",
"Linux 裡有許多與系統相關的操作需要有 root 的權限,哪個指令可以幫助我們切換成 root?":"su",
@c910335
c910335 / where.cr
Created April 17, 2018 10:15
Granite::ORM::Base.where
class Granite::ORM::Base
def self.where(**conditions)
params = [] of DB::Any
clause = String.build do |s|
s << "WHERE "
s << conditions.map do |k, v|
if v.nil?
"#{k} IS NULL"
else
params << v