help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any disadvantages of using put/get instead of defvar?


From: Tassilo Horn
Subject: Re: Any disadvantages of using put/get instead of defvar?
Date: Fri, 21 Feb 2014 12:56:06 +0100
User-agent: Gnus/5.13001 (Ma Gnus v0.10) Emacs/24.3.50 (gnu/linux)

Andreas Röhler <address@hidden> writes:

>> Well, I've measured my counter above versus a version using symbol
>> properties as you suggest:
>>
>>    (defun bar ()
>>      (let ((foo (or (get 'bar 'foo) 1)))
>>        (put 'bar 'foo (1+ foo))))
>>
>> My counter is way faster although it uses defvar and setq-local, so that
>> overhead is still small compared to looking up/putting a symbol
>> property.
>>
>
> BTW in earlier times a "let" was used.
> IIUC the way to make a function-local value now is "defvar" inside?

No, not at all.  `defvar' creates a global variable, no matter where
it's called.  Oleh's goal was just to keep a global variable and
function together, because that function is the only one using the
variable.

Another way would be

  (progn
    (defvar bar-foo 0)
    (defun bar ()
      ;; do stuff with bar-foo
      ))

which has the possible benefit that bar-foo is defined as soon as the
file is loaded instead being undefined until the first `bar' call.  And
the wrapping in a `progn' probably reduces the chance that you move
`bar' somewhere and forget `bar-foo'.

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]