13 Replies - 1388 Views - Last Post: 16 July 2013 - 12:25 PM

#1 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-March 13

Efficiency with coding

Posted 08 July 2013 - 01:08 PM

Hi all,

In University I lost a few marks with a program that I wrote because it wasn't as efficient as he liked it to be. For example I had an extra "for loop" which could be removed and he said that reduced it's efficency.

What I would like to know is where can you find out ways to improve the efficency of your code?
And how do you know if it's efficient?

Is it all about running as fast as it can or is it about having a program that takes up the least amount of memory as possible.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Efficiency with coding

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,254
  • Joined: 12-June 08

Re: Efficiency with coding

Posted 08 July 2013 - 01:13 PM

Quote

Is it all about running as fast as it can or is it about having a program that takes up the least amount of memory as possible.

Both.. and then there's also a factor of readability/maintainability...

Typically 'big o notation' is where you would like to start, but mostly it comes down to knowing the over arching effects of your loops, declarations, order of operations, etc as you mature as a developer.
Was This Post Helpful? 2
  • +
  • -

#3 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Efficiency with coding

Posted 08 July 2013 - 01:14 PM

They are two separate measures. Memory efficiency is one measure, the processing efficiency is another. In a lot of cases you can trade off one for the other.
Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Efficiency with coding

Posted 08 July 2013 - 01:15 PM

One way to start thinking about this is to take a course in algorithms and data structures. There's an algorithms course running on Coursera right now, but it's already a week in, so you'd have to work hard to catch up.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: Efficiency with coding

Posted 08 July 2013 - 01:16 PM

Go ahead and post your code, and let us critique it! Learning how to make your code more efficient comes with practice. This is probably as good a place to start as any.

Big-O is a good tool to describe efficiency, as is deriving the runtime function T(n) for your code. That's all a bit more advanced though.
Was This Post Helpful? 1
  • +
  • -

#6 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-March 13

Re: Efficiency with coding

Posted 08 July 2013 - 01:29 PM

Quote

Typically 'big o notation' is where you would like to start, but mostly it comes down to knowing the over arching effects of your loops, declarations, order of operations, etc as you mature as a developer.


For mathematics I've only got high-school level. In otherwords I got a GCSE in it but thats all. Wouldn't algorithms and Big O notation require a more advanced level or would it be okay?

Also where can I learn about Big O Notation.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8002
  • View blog
  • Posts: 13,711
  • Joined: 19-March 11

Re: Efficiency with coding

Posted 08 July 2013 - 01:33 PM

It relies a lot on discrete math, but there's nothing you can't work out on the way. And learning to think about algorithms can be a good way to start thinking about the math.

Quote

Also where can I learn about Big O Notation.


Wikipedia has a good overview.
https://en.wikipedia.../Big_O_notation
Was This Post Helpful? 1
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,254
  • Joined: 12-June 08

Re: Efficiency with coding

Posted 08 July 2013 - 01:34 PM

Quote

For mathematics I've only got high-school level. In otherwords I got a GCSE in it but thats all. Wouldn't algorithms and Big O notation require a more advanced level or would it be okay?


Sure? If you can understand how to reduce things, why the various 'times' are measured as they are - then no. Even if your math is a bit low you can try researching it without causing havoc, right?


Quote

Also where can I learn about Big O Notation.


The typical ways.. books.. teachers.. heck, even searching "big o notation" yields things like this:

http://rob-bell.net/...big-o-notation/
Was This Post Helpful? 1
  • +
  • -

#9 UnknownCodester  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-March 13

Re: Efficiency with coding

Posted 08 July 2013 - 01:36 PM

View Postjon.kiparsky, on 08 July 2013 - 01:33 PM, said:

It relies a lot on discrete math, but there's nothing you can't work out on the way. And learning to think about algorithms can be a good way to start thinking about the math.

Quote

Also where can I learn about Big O Notation.


Wikipedia has a good overview.
https://en.wikipedia.../Big_O_notation


Thank you... Can you check the run time in eclipse? Refactor and check the new runtime?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: Efficiency with coding

Posted 08 July 2013 - 01:39 PM

What you will probably be interested in is Computational Complexity. It presents a better case than simply comparing runtimes because it looks at your algorithm rather than the compiler and runtime optimizations that occur.
Was This Post Helpful? 0
  • +
  • -

#11 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,206
  • Joined: 12-May 09

Re: Efficiency with coding

Posted 08 July 2013 - 03:59 PM

Just to add to what macosxnerd101 said, runtime isn't entirely reliable because it can be impacted by many things, including runtime optimizations:

-differs from machine to machine

-if you have external dependencies, they can impact runtime depending on their load/timings

-if your machine suddenly has things to do or gains memory or the state changes in any way, this can alter the runtime

-guess and check isn't nearly as effective as being able to reason about performance. This is especially the case when it comes to long-running algorithms. If the fastest implementation ever takes an hour, you don't want to have to run your code over and over and see if it takes 4 hours or 2 hours this time. You want to be able to reason about how quick your algorithm is.

-growth rate allows a better bound for comparison than runtime. Runtime gives you one number, for a given input. Growth rate, which is what is measured in Computational Complexity, gives you a function, so you can think about how your algorithm performs with inputs of varying sizes. In reality, you care about the growth of your runtime as your data size increases, and runtime can't give you that without repeated runs. Big O can.

This post has been edited by xclite: 08 July 2013 - 04:00 PM

Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Efficiency with coding

Posted 09 July 2013 - 12:36 AM

And then there are also some cases where Big O notation will tell you one thing when comparing two algorithms or data structures, but actually running the code will give you a completely inverted result because Big O notation doesn't take into account the cost of memory cache misses and too much branching.

http://www.codeproje...er-ever-EVER-us
http://www.youtube.c...h?v=YQs6IC-vgmo
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Efficiency with coding

Posted 09 July 2013 - 04:09 AM

Simply, everything you do takes resources: both storage and CPU cycles. Depending on the problem, efficiency will often involve best use of just one of these two resources. Though the goal is always to minimize the use of both.

As long as you keep any mind that EVERYTHING you do has an associated cost, you should be able to spot the more efficient code. Note, when you call a function you didn't write, all bets are off.

From the programmer's perspective, it's acceptable to have slightly less efficient code if it's significantly more maintainable. Object Oriented Programming epitomizes this trade off.

Calling a function has a small amount of overhead; some storage and a jump. However, using functions drastically increases readability and maintainability, so the loss is almost always considered acceptable. The rare exception to this would be something like a screen refresh for a game, where every millisecond counts.
Was This Post Helpful? 1
  • +
  • -

#14 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Efficiency with coding

Posted 16 July 2013 - 12:25 PM

Try learning about refactoring. And study efficient programs of other programmers. With time, you'll be able to easily see ways to improve the efficiency of your programs.

It's not an either-or situation. Both time and memory that a program consumes is important. From what I've heard though memory isn't as important. Back in the day it was because we didn't have as much memory has we do now. There were certain memory constraints for programs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1