Skip to content

Instantly share code, notes, and snippets.

@aminya
Last active February 13, 2020 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminya/4b6e6db1147e63727cffc781633a8e0b to your computer and use it in GitHub Desktop.
Save aminya/4b6e6db1147e63727cffc781633a8e0b to your computer and use it in GitHub Desktop.
using Faker
function genstring(num)
list=Vector{String}(undef, num)
for i=1:num
list[i]= Faker.lexify("???????")
end
return list
end
using Distributions
list = genstring(2000)
random_indices = Int64.(round.(rand(Uniform{Int64}(1, 2000),100)))
input = list[random_indices]
regex_list = Regex.(list)
function check(regex_list, input)
input_len=length(input)
icon_indices = Vector{Int64}(undef, input_len)
@inbounds for j=1:input_len
for i=1:length(regex_list)
if match(regex_list[i],input[j]) !== nothing
# println(i,"-",j) # to check functionality
icon_indices[j] = i
break
end
end
end
return icon_indices
end
function check_simd(regex_list, input)
input_len=length(input)
icon_indices = Vector{Int64}(undef, input_len)
@simd for j=1:input_len
@inbounds icon_indices[j] = findfirst(x-> match(x, input[j]) !== nothing, regex_list)
end
return icon_indices
end
check(regex_list, input)
check_simd(regex_list, input)
using BenchmarkTools
@btime check($regex_list, $input)
# 15.770 ms (401 allocations: 24.31 KiB)
@btime check_simd($regex_list, $input)
# 15.824 ms (401 allocations: 24.31 KiB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment