[Tutor] global variables/constants versus volatile variables/constants
S Tareq
stareq13 at yahoo.com
Sat Jun 14 21:37:56 CEST 2014
ssss
On Friday, 13 June 2014, 12:45, Steven D'Aprano <steve at pearwood.info> wrote:
On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote:
> Hi All!
> Hope everyone is
well.
>
> In my code there are many dictionaries and lists which are used in various
> functions. Is it better/pythonic/efficient to have these inside the
> function itself or declared at the beginning of the program in which case
> they will be global? They are all read only. I understand that global
> constants and variable have memory allocated to them but when declared
> inside a function are created on the fly, used and discarded. Please
> enlighten me further on this and correct me if i'm wrong.
A good question.
Depending on the size of these dictionaries and lists, worrying about
efficiency here may be premature optimization. As they say:
"We should forget about small efficiencies, say about 97% of
the time: premature optimization is the root of all evil."
-- Donald Knuth
"The First Rule of Program
Optimization: Don't do it. The
Second Rule of Program Optimization (for experts only!):
Don't do it yet." -- Michael A. Jackson
"More computing sins are committed in the name of efficiency
(without necessarily achieving it) than for any other single
reason — including blind stupidity." -- W.A. Wulf
If these lists and dicts are small, say, fewer than a dozen items, the
time to create and destroy them is probably trivial, especially if
you construct them from constant literals. In that case, it's a
matter of personal taste whether you prefer them as global constants or
local to a function.
But if it takes a long time to build the list, then you definitely
should move it outside the function and perform the initialisation step
only once:
# This may take a while...
PRIMES = [prime(i) for i in range(1,
1000001)]
If your lists really are read-only, then you should consider turning
them into tuples:
# Not this:
SOME_ITEMS = ["this", "that", "another"]
# Better:
SOME_ITEMS = ("this", "that", "another")
One advantage of the tuple is that in recent versions of Python, the
tuple may be compiled as a constant instead of being built at runtime.
This is from Python 2.7:
py> from dis import dis
py> code = compile("x = [2, 4, 8]", "", "exec")
py> dis(code)
1 0 LOAD_CONST 0 (2)
3 LOAD_CONST 1 (4)
6 LOAD_CONST 2 (8)
9 BUILD_LIST
3
12 STORE_NAME 0 (x)
15 LOAD_CONST 3 (None)
18 RETURN_VALUE
py> code = compile("x = (2, 4, 8)", "", "exec")
py> dis(code)
1 0 LOAD_CONST 4 ((2, 4, 8))
3 STORE_NAME 0 (x)
6 LOAD_CONST 3 (None)
9 RETURN_VALUE
--
Steven
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140614/2bcccb24/attachment-0001.html>
More information about the Tutor
mailing list