Skip to content

Instantly share code, notes, and snippets.

@UncleBill

UncleBill/gist:9482014

Last active Aug 29, 2015
Embed
What would you like to do?
fun! s:use_eval(co)
let c = a:co
let r = eval('0x'.c[0].c[1])
let g = eval('0x'.c[2].c[3])
let b = eval('0x'.c[4].c[5])
return [r,g,b]
endfunction
fun! s:without_eval(co)
let c = a:co
let r = str2nr(c[0:1],16)
let g = str2nr(c[2:3],16)
let b = str2nr(c[4:],16)
return [r,g,b]
endf
function! s:nr2hex(nr)
let n = a:nr
let r = ""
while n
let r = '0123456789ABCDEF'[n % 16] . r
let n = n / 16
endwhile
return r
endfunction
let start = reltime()
for i in range(0x100)
" let color = ( '000000'.s:nr2hex(i) )[-6:]
let color = repeat( ('00'.s:nr2hex(i))[-2:], 3 )
let l1 = s:use_eval(color)
let l2 = s:without_eval(color)
if l1 != l2
echo "Failed at: ".i
endif
endfor
echo "time cost".reltimestr(reltime(start))
echo "All pasted!"
let color = '012345'
let times = 1000
echo "Results:"
echo "-----------------------------------------"
echo "run: ".times." times"
fun! s:use_eval(co)
let c = a:co
let r = eval('0x'.c[0].c[1])
let g = eval('0x'.c[2].c[3])
let b = eval('0x'.c[4].c[5])
return [r,g,b]
endfunction
fun! s:without_eval(co)
let c = a:co
let r = str2nr(c[0:1],16)
let g = str2nr(c[2:3],16)
let b = str2nr(c[4:],16)
return [r,g,b]
endf
let start = reltime()
for i in range(times)
call s:use_eval(color)
endfor
echo 'use eval:'.reltimestr(reltime(start))
let start = reltime()
for i in range(times)
call s:without_eval(color)
endfor
echo 'without eval:'.reltimestr(reltime(start))
Results:
-----------------------------------------
run: 1000 times
use eval: 0.022386
without eval: 0.019251
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.