Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

High-performance Ruby: faster Symbol.to_s

Here's something that I found useful for shaving a few microseconds off. The performance gain ranges between 10% and 35%! YMMV.

Note: updated with suggestion by trans.

class Symbol
  def to_s
    @str_rep || (@str_rep = id2name.freeze)
  end
end

gen-sym

REBOL [
    Title: "Unique symbol generator"
]

gen-sym-ctx: context [
    prefix: "&"
    counter: 0
    clear: does [counter: 0]
    set-prefix: func [value] [prefix: form value]
    set 'gen-sym does [
        counter: counter + 1
        to word! join prefix counter
    ]
]

repeat i 5 [print gen-sym]
gen-sym-ctx/set-prefix 'G
gen-sym-ctx/clear
repeat i 5 [print gen-sym]
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS