473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Any way to refactor this?

Setting aside, for the moment, the utility of this method or even if
there's a better way, I'm wondering if this is an efficient way to do
it. I admit, there was some copying and pasting, which is what prompts
me to ask the question. Here's the method. (I hope it looks ok, because
it looks really weird for me right now)

def _create_3D_xhatches():
for x in xrange(-axis_length, axis_length + 1):
if x == 0: continue
visual.cylinder(pos=(x,-hatch_length,0),
axis=(0,hatch_length*2,0), radius=hatch_radius)
visual.cylinder(pos=(x,0,-hatch_length),
axis=(0,0,hatch_length*2), radius=hatch_radius)
visual.cylinder(pos=(-hatch_length,x,0),
axis=(hatch_length*2,0,0), radius=hatch_radius)
visual.cylinder(pos=(0,x,-hatch_length),
axis=(0,0,hatch_length*2), radius=hatch_radius)
visual.cylinder(pos=(-hatch_length,0,x),
axis=(hatch_length*2,0,0), radius=hatch_radius)
visual.cylinder(pos=(0,-hatch_length,x),
axis=(0,hatch_length*2,0), radius=hatch_radius)

Since each call to cylinder requires a slightly different format, I
figured I had to do it this way.

Thanks.
Apr 13 '07 #1
7 1067
John Salerno wrote:
Setting aside, for the moment, the utility of this method or even if
there's a better way, I'm wondering if this is an efficient way to do
it. I admit, there was some copying and pasting, which is what prompts
me to ask the question. Here's the method. (I hope it looks ok, because
it looks really weird for me right now)

def _create_3D_xhatches():
for x in xrange(-axis_length, axis_length + 1):
if x == 0: continue
visual.cylinder(pos=(x,-hatch_length,0),
axis=(0,hatch_length*2,0), radius=hatch_radius)
visual.cylinder(pos=(x,0,-hatch_length),
axis=(0,0,hatch_length*2), radius=hatch_radius)
visual.cylinder(pos=(-hatch_length,x,0),
axis=(hatch_length*2,0,0), radius=hatch_radius)
visual.cylinder(pos=(0,x,-hatch_length),
axis=(0,0,hatch_length*2), radius=hatch_radius)
visual.cylinder(pos=(-hatch_length,0,x),
axis=(hatch_length*2,0,0), radius=hatch_radius)
visual.cylinder(pos=(0,-hatch_length,x),
axis=(0,hatch_length*2,0), radius=hatch_radius)

Since each call to cylinder requires a slightly different format, I
figured I had to do it this way.

Thanks.
Your parameters don't follow an unambiguous pattern. Probably best is to
code the parameters as a set of tuples and iterate over them.

James
Apr 13 '07 #2
John Salerno a écrit :
Setting aside, for the moment, the utility of this method or even if
there's a better way, I'm wondering if this is an efficient way to do
it. I admit, there was some copying and pasting, which is what prompts
me to ask the question. Here's the method. (I hope it looks ok, because
it looks really weird for me right now)

def _create_3D_xhatches():
for x in xrange(-axis_length, axis_length + 1):
if x == 0: continue
visual.cylinder(pos=(x,-hatch_length,0),
axis=(0,hatch_length*2,0), radius=hatch_radius)
visual.cylinder(pos=(x,0,-hatch_length),
axis=(0,0,hatch_length*2), radius=hatch_radius)
visual.cylinder(pos=(-hatch_length,x,0),
axis=(hatch_length*2,0,0), radius=hatch_radius)
visual.cylinder(pos=(0,x,-hatch_length),
axis=(0,0,hatch_length*2), radius=hatch_radius)
visual.cylinder(pos=(-hatch_length,0,x),
axis=(hatch_length*2,0,0), radius=hatch_radius)
visual.cylinder(pos=(0,-hatch_length,x),
axis=(0,hatch_length*2,0), radius=hatch_radius)

Since each call to cylinder requires a slightly different format, I
figured I had to do it this way.
From a purely efficiency POV, there are some obviously possible
improvements. The first one is to alias visual.cylinder, so you save on
lookup time. The other one is to avoid useless recomputation of
-hatch_length and hatch_length*2.

def _create_3D_xhatches():
cy = visual.cylinder
for x in xrange(-axis_length, axis_length + 1):
if x == 0: continue
b = -hatch_length
c = hatch_length*2
cy(pos=(x, b, 0), axis=(0, c, 0), radius=hatch_radius)
cy(pos=(x, 0, b), axis=(0, 0, c), radius=hatch_radius)
cy(pos=(b, x, 0), axis=(c, 0, 0), radius=hatch_radius)
cy(pos=(0, x, b), axis=(0, 0, c), radius=hatch_radius)
cy(pos=(b, 0, x), axis=(c, 0, 0), radius=hatch_radius)
cy(pos=(0, b, x), axis=(0, c, 0), radius=hatch_radius)

A second step would be to try and generate the permutations by code
instead of writing them all by hand, but I suppose the order is
significant...
There's still an obvious pattern, which is that the position of 'c' in
the axis tuple mirrors the position of 'b' in the pos tuple. There might
be some way to use this to let the computer handle some part of the
repetition...

My 2 cents...
Apr 13 '07 #3
James Stroud:
Probably best is to code the parameters as
a set of tuples and iterate over them.
I agree. Before:

def _create_3D_xhatches(...pass more parameters here...):
for x in xrange(-axis_length, axis_length + 1):
if x == 0:
continue
visual.cylinder(pos=(x, -hatch_length, 0),
axis=(0, hatch_length*2, 0),
radius=hatch_radius)
visual.cylinder(pos=(x, 0, -hatch_length),
axis=(0, 0, hatch_length*2),
radius=hatch_radius)
visual.cylinder(pos=(-hatch_length, x, 0),
axis=(hatch_length*2, 0, 0),
radius=hatch_radius)
visual.cylinder(pos=(0, x, -hatch_length),
axis=(0, 0, hatch_length*2),
radius=hatch_radius)
visual.cylinder(pos=(-hatch_length, 0, x),
axis=(hatch_length*2, 0, 0),
radius=hatch_radius)
visual.cylinder(pos=(0, -hatch_length, x),
axis=(0, hatch_length*2, 0),
radius=hatch_radius)
And after:

def _create_3D_xhatches(...pass more parameters here...):
hl2 = hatch_length * 2
for x in xrange(-axis_length, axis_length + 1):
if x == 0:
continue
params = [[(x, -hatch_length, 0), (0, hl2, 0)],
[(x, 0, -hatch_length), (0, 0, hl2)]
[(-hatch_length, x, 0), (hl2, 0, 0)],
[(0, x, -hatch_length), (0, 0, hl2)],
[(-hatch_length, 0, x), (hl2, 0, 0)],
[(0, -hatch_length, x), (0, hl2, 0)]]
for pos, axis in params:
visual.cylinder(pos=pos, axis=axis, radius=hatch_radius)

More cleaning can be done.

Bye,
bearophile

Apr 13 '07 #4
John Salerno wrote:
Setting aside, for the moment, the utility of this method or even if
there's a better way, I'm wondering if this is an efficient way to do
it. I admit, there was some copying and pasting, which is what prompts
me to ask the question. Here's the method. (I hope it looks ok, because
it looks really weird for me right now)
Thanks guys. I'm going to look over all this and see what I can do.
Apr 18 '07 #5
Bruno Desthuilliers wrote:
From a purely efficiency POV, there are some obviously possible
improvements. The first one is to alias visual.cylinder, so you save on
lookup time. The other one is to avoid useless recomputation of
-hatch_length and hatch_length*2.

def _create_3D_xhatches():
cy = visual.cylinder
for x in xrange(-axis_length, axis_length + 1):
if x == 0: continue
b = -hatch_length
c = hatch_length*2
cy(pos=(x, b, 0), axis=(0, c, 0), radius=hatch_radius)
cy(pos=(x, 0, b), axis=(0, 0, c), radius=hatch_radius)
cy(pos=(b, x, 0), axis=(c, 0, 0), radius=hatch_radius)
cy(pos=(0, x, b), axis=(0, 0, c), radius=hatch_radius)
cy(pos=(b, 0, x), axis=(c, 0, 0), radius=hatch_radius)
cy(pos=(0, b, x), axis=(0, c, 0), radius=hatch_radius)
Doesn't this call to "cy" still call the function multiple times?
May 2 '07 #6
On Wed, 02 May 2007 11:37:14 -0400, John Salerno wrote:
Bruno Desthuilliers wrote:
> From a purely efficiency POV, there are some obviously possible
improvements. The first one is to alias visual.cylinder, so you save on
lookup time. The other one is to avoid useless recomputation of
-hatch_length and hatch_length*2.

def _create_3D_xhatches():
cy = visual.cylinder
for x in xrange(-axis_length, axis_length + 1):
if x == 0: continue
b = -hatch_length
c = hatch_length*2
cy(pos=(x, b, 0), axis=(0, c, 0), radius=hatch_radius)
cy(pos=(x, 0, b), axis=(0, 0, c), radius=hatch_radius)
cy(pos=(b, x, 0), axis=(c, 0, 0), radius=hatch_radius)
cy(pos=(0, x, b), axis=(0, 0, c), radius=hatch_radius)
cy(pos=(b, 0, x), axis=(c, 0, 0), radius=hatch_radius)
cy(pos=(0, b, x), axis=(0, c, 0), radius=hatch_radius)

Doesn't this call to "cy" still call the function multiple times?
I'm not sure I understand what you mean, but here goes anyway...

Well, yes, but you have to call it six times per loop, with six different
sets of arguments, that's why there are six calls to it. I don't think
there's any way to reduce that (although if there is, the Original Poster
would probably love to hear about it).

Bruno's code has two micro-optimizations. The first is to avoid
looking up visual.cylinder each time (six times the number of loops) and
instead only look it up once. If axis_length is (say) 100, you save 1200
name look-ups of arbitrary complexity.

(Perhaps visual inherits from Klass, which inherits from Spam, which
inherits from Parrot, which inherits from Foo, which inherits from Bar,
which has a method "cylinder". Name look-ups can be time consuming.)

The second is to avoid calculating -hatch_length and hatch_length*2 for
each call, but to calculate them only once per loop. Again, only a
micro-optimization, but arithmetic in Python is more work than in (say) C,
because of the whole object oriented framework. So if you can avoid having
to look up hatch_length.__mul__ repeatedly, you may see a small but
significant time saving.
--
Steven.

May 2 '07 #7
Steven D'Aprano wrote:
Bruno's code has two micro-optimizations. The first is to avoid
looking up visual.cylinder each time (six times the number of loops) and
instead only look it up once.
Oh I see. I was thinking the optimization had something to do with
*calling* the function less often, but that's different from the actual
lookup I suppose. :)
May 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: lawrence | last post by:
Dear Peter, Do we know anyone else who writes PHP code? There is too much work to do, especially if Costin and I are going to join our software together. The easiest way for us to join our...
0
by: Cyanbane | last post by:
Does anyone know if a Demo exists of Refactor! for Vis Studio 2k3 (C#)? It looks like you can order the full version for $99, but I was wondering if you could demo it before. Anyone have any...
1
by: moondaddy | last post by:
I'm running VS 2005 pro vb.net and started to experiment with refactoring. I selected some code, right clicked and selected Refactor / Extract Method. The Refactor popup window appeared. This was...
1
by: PJ | last post by:
Why does the refactor menu not show up in vb.net project while it shows up in c# projects? Thanks ~PJ
12
by: lh84777 | last post by:
Hello, i'm looking for this behaviour and i write a piece of code which works, but it looks odd to me. can someone help me to refactor it ? i would like to walk across a list of items by...
1
by: Steven Nagy | last post by:
Howdy folks. So I have an App from 1.1 that I converted to 2.0. I now want to refactor a bit. One of the things I want to do is create master page for the header, footer and menu that exists on...
2
by: V | last post by:
I've been using VS2005 for a mixed C# / C++ project. Somewhere along the line it seems that 'refactor' on the context menu disappeared for C++ projects. I do believe that this was working at one...
1
by: Andreas Bauer | last post by:
Hi, I have a Data Access Layer where I have to change the method names and signatures. This works fine with the DAL project, but as far as I can see, the refactor function won´t update the...
2
by: Smokey Grindel | last post by:
I know in C# under VS2005 there is a nifty "refactor" menu that has a few useful commands that i'd love to have in VB.NET.. anyone know how to get similar functionality in VB.NET 2005? add-ons or...
0
by: =?Utf-8?B?S2luZXRpYyBKdW1wIEFwcGxpZmUgZm9yIC5ORVQg | last post by:
Refactor .NET code using Visual Studio and Refactor! Pro Modifying the existing source code without changing its functionality is called refactoring of Source Code. Visual Studio gives us a lots of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.