Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Learning Python: efficiency question
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-21-2012, 04:30 PM PM User | #1 SoccerGee New to the CF scene   Join Date: Jan 2012 Posts: 5 Thanks: 0 Thanked 0 Times in 0 Posts Learning Python: efficiency question I'm new to Python and a beginner at programming in general. Is there an efficiency difference between Code A and Code B? Code A: Code: ```def verbing(s): if len(s) > 3 and s[-3:] == 'ing': return str(s) + 'ly' if len(s) > 3: return str(s) + 'ing' if len(s) <= 3: return str(s)``` Code B: Code: ```def verbing(s): if len(s) >= 3: if s[-3:] != 'ing': s = s + 'ing' else: s = s + 'ly' return s```
 01-22-2012, 04:25 PM PM User | #2 Apothem Regular Coder     Join Date: Mar 2008 Posts: 380 Thanks: 36 Thanked 25 Times in 25 Posts I would think so, yes. 1. Code A is calling len() a lot more. At worse, you're calling it 3 times. If the string is quite large, it'd be quite inefficient. 2. Code A has more conditions. That's all I an think of.
01-23-2012, 04:18 AM   PM User | #3
BluePanther
Senior Coder

Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
Code B is better, for all the reasons posted above. It also shows better logic, through your use of 1 return of s.

But, the code does slightly different things. Code A won't append anything to s if s is = to 3, whereas Code B will. Is this a mistake?
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
``` // Replace this if(isset(\$_POST['submitButton'])) // With this if(!empty(\$_POST)) // Then check for values/forms. Some IE versions don't send the submit button  ```
Quote:
 Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

 Bookmarks

 Tags efficiency

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:31 PM.