New topic: 

Methods - more efficient, or just another step in code?

<http://forums.realsoftware.com/viewtopic.php?t=34223>

         Page 1 of 1
   [ 4 posts ]                 Previous topic | Next topic          Author  
Message        Wolfsnap          Post subject: Methods - more efficient, or 
just another step in code?Posted: Thu Jun 10, 2010 7:31 pm                      
   
Joined: Thu May 10, 2007 10:25 pm
Posts: 54                Just curious...
I have a project that is very code-heavy in the Paint event of a canvas 
(depending on what listing you click on in a hierarchical listbox). I'm using a 
Select Case statement to determine what the user has clicked on, then designing 
the Paint Event for the canvas. It's getting cumbersome in one batch of code - 
seems like it would be easier to create a method for each Case, and refer top 
that method - "Case XYZ" - Method "DrawThis"

But it seems like an extra set of code - another step that doesn't need to be 
there (the re-direction of the code would be 1 more step for the process). (I 
know - we're talking about nano-seconds here - but I need this thing to run as 
efficiently as possible - but methods would make the project MUCH EASIER to 
keep track of as far as the coding goes)

On the same (kinda) subject - How many Properties are too many? Does the number 
of properties slow down performance, or are they only called when needed...?

AND - if I have several windows that use the same method (for example, an 
"EscapeSQLData" method as found in the Database example) - is it more efficient 
to add that method to each individual window, or refer to that method (assuming 
it's global) from the original window? (just trying NOT to have to write the 
"xyzWindow.EscapeSQLData" - as opposed to just "EscapeSQLData")

Bottom line - how does the number of properties and methods effect performance? 
  
                             Top                brisance          Post subject: 
Re: Methods - more efficient, or just another step in code?Posted: Thu Jun 10, 
2010 8:52 pm                         
Joined: Tue Oct 06, 2009 2:38 am
Posts: 252                For performance optimization, use a profiler to 
analyze which function takes the most time and/or gets called most frequently. 
There are many tricks to employ, but there are also many gotchas and 
case-specific situations where a perceived optimization is actually a 
regression due to differences in CPU architecture, compiler design etc. So 
while there are many suggestions and guidelines, the only way to know for sure 
is to test extensively for your specific situation.

In the first scenario, besides the standard Select-Case, If-Else flow control 
keywords, depending on how many tests you need to run, a lookup table/hash 
table may be a better solution.

For the second scenario, if the function is called from multiple places, stick 
it in a module in order to promote code reuse and prevent code maintenance 
problems. Refer to Pragmatic Programmer and the Don't Repeat Yourself 
principle.      
_________________
Mike Ash: Getting Answers
  
                             Top                 doofus          Post subject: 
Re: Methods - more efficient, or just another step in code?Posted: Thu Jun 10, 
2010 10:44 pm                                 
Joined: Thu Sep 10, 2009 2:50 am
Posts: 5
Location: Santa Cruz, CA, USA                Wolfsnap wrote:Just curious...
I have a project that is very code-heavy in the Paint event of a canvas 
(depending on what listing you click on in a hierarchical listbox). I'm using a 
Select Case statement to determine what the user has clicked on, then designing 
the Paint Event for the canvas. It's getting cumbersome in one batch of code...
<snip>
Wolfsnap wrote:Bottom line - how does the number of properties and methods 
effect performance?

I went down this rabbit hole several years ago. I was rendering a picture, 
wanted it as fast as possible and had the notion that the 'overhead' of a 
method call or non-local data was something I could take out. I ended up with a 
15 page method, the first half loads everything to local variables and the 
second half is a mess of nested if/else and selects. Then I got ideas of 
rendering variations so I made a method for each style, each as monolithic as 
the first. That app is the biggest mess I've ever made. I mean code wise it's a 
mess, the app is stable and used lots, but I reached a point where it's just 
too cumbersome to dig in and untangle certain parts.

In the end I think the monolith methods are slower because the compiler 
probable chokes trying to optimize it; it's certainly not maintainable.

Methods/Objects are your friend. Make them the way that's easiest/clearest for 
you to use them in code. If it's not fast enough at least the structure is 
there so it's easier to find the bottlenecks.

The only way to know if something is faster is to time/profile it. Source code 
to machine code is not a 1 to 1 translation, ie the compiler can jumble things 
around trying to optimize. -w   
                             Top                timhare          Post subject: 
Re: Methods - more efficient, or just another step in code?Posted: Thu Jun 10, 
2010 10:52 pm                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 7880
Location: Portland, OR  USA                Nothing that you mentioned is going 
to cause any performance problems.  Just go ahead and write clear, maintainable 
code and don't worry about it.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 4 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to