[DB-SIG] Optional DB API Extensions
Federico Di Gregorio
fog@debian.org
24 Oct 2001 15:56:47 +0200
--=-4CRMPXf7UAX0CV8F+VQ8
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Wed, 2001-10-24 at 15:41, Magnus Lyck=E5 wrote:
> Just as a little annoying suggestion: If you do want your
> generic high performance code inside this big loop, you
> could instead of
>=20
> ---
> while biiiig loooop:
> ret =3D might.work()
> if ret =3D=3D -1:
> ret =3D SomeOtherWayOfFindingOut()
> use(ret)
> ---
>=20
> do it like this
> ---
> itWorks =3D 1
> while biiiig loooop:
> if itWorks:
> try:
> ret =3D might.work()
> except NotImplemented:
> itWorks =3D 0
> ret =3D SomeOtherWayOfFindingOut()
> else:
> ret =3D SomeOtherWayOfFindingOut()
> use(ret)
> ---
>=20
> You will only get one exception. You don't call might.work()
> more than one time if it fails. You also get as many ifs in
> both cases. This should be as fast if might.works() is in
> order, and faster if it isn't.
exceptions are there for exceptional circumstances (have you ever tried
timing ifs and excepts? ifs are at least 30x faster in python [1.5.x,
when i did the experiment]).
anyway, while talking about coding styles (a little OT, i know) i think
it is better to cleany separate the feature-peeking part from the rest
of the code. i.e.,
if might.work() =3D=3D None: feature_x_present =3D 0
else: feature_x_present =3D 1
...
while biiig looop:
if feature_x_present:
use(might.work())
else:
use(SomeOtherWayOfFindingOut())
but all that is personal taste, off curse.=20
have fun,
federico
--=20
Federico Di Gregorio
Debian GNU/Linux Developer & Italian Press Contact fog@debian.org
INIT.D Developer fog@initd.org
A short story: I want you. I love you. I'll miss you. -- Me
--=-4CRMPXf7UAX0CV8F+VQ8
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iEYEABECAAYFAjvWyJ8ACgkQvcCgrgZGjevPegCeMoOFLeUCdA319NLGt7LCRerr
7ZkAnROC+ED7/BreEHqOy3QYbmCP5OR6
=sntv
-----END PGP SIGNATURE-----
--=-4CRMPXf7UAX0CV8F+VQ8--