Last active
July 12, 2017 02:40
-
-
Save Nephos/ad292a3e2acc9201e6ea6342eb85dacb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "benchmark" | |
def f2(str) | |
str.gsub(/\[\[([[:alnum:]\-\/]+)\]\]/, "[\\1](/pages/\\1)") | |
end | |
def f3(str) | |
cursor = 0 | |
String.build do |b| | |
while cursor < str.size | |
if link_begin = str.index("[[", cursor) | |
text_begin = link_begin + 2 | |
if link_end = str.index("]]", text_begin) | |
text_end = link_end - 1 | |
text = str[text_begin..text_end] | |
b << str[cursor..(link_begin - 1)] | |
b << "[" << text << "](/pages/" << text << ")" | |
cursor = link_end + 2 | |
else | |
b << str[cursor..-1] | |
cursor = str.size | |
end | |
else | |
b << str[cursor..-1] | |
cursor = str.size | |
end | |
end | |
end | |
end | |
def f4(str) | |
cursor = 0 | |
String.build do |b| | |
while cursor < str.size | |
if (link_begin = str.index('[', cursor)) && str[link_begin + 1] == '[' | |
text_begin = link_begin + 2 | |
if (link_end = str.index(']', text_begin)) && str[link_end + 1] == ']' | |
text_end = link_end - 1 | |
text = str[text_begin..text_end] | |
b << str[cursor..(link_begin - 1)] | |
b << '[' << text << "](/pages/" << text << ')' | |
cursor = link_end + 2 | |
else | |
b << str[cursor..-1] | |
cursor = str.size | |
end | |
else | |
b << str[cursor..-1] | |
cursor = str.size | |
end | |
end | |
end | |
end | |
PART2 = "](/pages/" | |
COPY1 = Pointer(UInt8).malloc(2048) | |
COPY2 = Pointer(UInt8).malloc(2048) | |
def f5(str) | |
cursor = 0 | |
String.build do |b| | |
while cursor < str.size | |
if (link_begin = str.index('[', cursor)) && str[link_begin + 1] == '[' | |
text_begin = link_begin + 2 | |
if (link_end = str.index(']', text_begin)) && str[link_end + 1] == ']' | |
text_end = link_end - 1 | |
# text | |
size = text_end - text_begin | |
# raise "STOP: not enough buffer" if size > 2047 | |
ptr = (str.to_unsafe + text_begin) | |
ptr.copy_to(COPY1, size + 1) | |
text = String.new ptr, size + 1 | |
# before the link | |
size = (link_begin - 1) - cursor | |
# raise "STOP: not enough buffer" if size > 2047 | |
ptr = (str.to_unsafe + cursor) | |
ptr.copy_to(COPY2, size + 1) | |
before_link = String.new ptr, size + 1 | |
b << before_link | |
b << '[' << text << "](/pages/" << text << ')' | |
cursor = link_end + 2 | |
else | |
b << str[cursor..-1] | |
cursor = str.size | |
end | |
else | |
b << str[cursor..-1] | |
cursor = str.size | |
end | |
end | |
end | |
end | |
STR = "this is a test with [[my-page]] and co" | |
Benchmark.ips do |x| | |
x.report("f2") { f2 STR } | |
x.report("f3") { f3 STR } | |
x.report("f4") { f4 STR } | |
x.report("f5") { f5 STR } | |
end | |
pp f2 STR | |
pp f3 STR | |
pp f4 STR | |
pp f5 STR |
Author
Nephos
commented
Jul 12, 2017
I don't think I can manage to get more performances than f4. I could be cool to do not have to allocate new sub-strings
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment