how to measure the Efficiency of code.

  • Follow


Hello

I know about profile but is there any other tool available
to measure whether the code is efficient. Clock? Tic?

Uwe Brauer 
0
Reply oub (83) 2/16/2010 11:11:52 AM

On 16 Feb, 12:11, Uwe Brauer <o...@mat.ucm.es> wrote:
> Hello
>
> I know about profile but is there any other tool available
> to measure whether the code is efficient. Clock? Tic?

Define what you mean by 'efficient':

- Run time?
- Memory footprint?
- Maintenance costs?
- Time to development?

Rune
0
Reply allnor (8474) 2/16/2010 11:19:12 AM


>>>>> On Tue, 16 Feb 2010 03:19:12 -0800 (PST), Rune Allnor <allnor@tele.ntnu.no> wrote:

   > On 16 Feb, 12:11, Uwe Brauer <o...@mat.ucm.es> wrote:
   >> Hello
   >> 
   >> I know about profile but is there any other tool available
   >> to measure whether the code is efficient. Clock? Tic?

   > Define what you mean by 'efficient':

   > - Run time?
   > - Memory footprint?
A combination of these two.

Uwe 
0
Reply oub (83) 2/16/2010 11:40:28 AM

On 16 Feb, 12:40, Uwe Brauer <o...@mat.ucm.es> wrote:
> >>>>> On Tue, 16 Feb 2010 03:19:12 -0800 (PST), Rune Allnor <all...@tele.=
ntnu.no> wrote:
>
> =A0 =A0> On 16 Feb, 12:11, Uwe Brauer <o...@mat.ucm.es> wrote:
> =A0 =A0>> Hello
> =A0 =A0>>
> =A0 =A0>> I know about profile but is there any other tool available
> =A0 =A0>> to measure whether the code is efficient. Clock? Tic?
>
> =A0 =A0> Define what you mean by 'efficient':
>
> =A0 =A0> - Run time?
> =A0 =A0> - Memory footprint?
> A combination of these two.

If such factors matter at all - forget about matlab.

Matlab is an interpreted language that expresses everything
and anything in terms of matrices and arrays.

Being interpreted means that a lot of stuff has to be checked
at run-time (like the number and types of arguments to
functions) that a compiled language would check at compile time.

Matrix semantics means that one's choise of dat astructures
is seriously restricted, in turn meaning that a number of
common tasks are implemented very inefficiently.

The one area where matlab actually has a forte is linear
algebra. If your application is dominated by linear algebra
operations, mtlab is hard to beat.

However, that advantage is all too often countered by its
inefficiency in all the other aspects of computing.

I use C++ for the heavy-duty work. It is not uncommon to
find C++ programs to be a factor 10 - 50 -100 faster than
the matlab counterpart, depending on what the program does.

Rune
0
Reply allnor (8474) 2/16/2010 12:06:50 PM

>>>>> On Tue, 16 Feb 2010 04:06:50 -0800 (PST), Rune Allnor <allnor@tele.ntnu.no> wrote:

   >> 
   >> � �> - Run time?
   >> � �> - Memory footprint?
   >> A combination of these two.

   > If such factors matter at all - forget about matlab.

Well yes and no. I agree with what you say but I am bound to
Matlab, since it is much easier to code for numerical work
(at least this is my impression) and can be easier debugged,
has nice graphical interface and a lot of built in
functions.


   > Matlab is an interpreted language that expresses everything
   > and anything in terms of matrices and arrays.

So let me rephrase my original question then: In the matlab
environment, what are the most common errors to be avoided
in order that the code does not get to un efficient?

   > I use C++ for the heavy-duty work. It is not uncommon to
   > find C++ programs to be a factor 10 - 50 -100 faster than
   > the matlab counterpart, depending on what the program does.

Ok if that is for say 3 dimensional partial diff eq matlab
is not for you, but frankly C and relatives I find sort of
....
Ok I don't want to start a flame war.


Uwe 
0
Reply oub (83) 2/16/2010 12:26:29 PM

On 16 Feb, 13:26, Uwe Brauer <o...@mat.ucm.es> wrote:

> Ok if that is for say 3 dimensional partial diff eq matlab
> is not for you, but frankly C and relatives I find sort of
> ...

Here you are at the core of your choise: Using matlab because
you as user find it more convenient to interact with the
available functionality than the casse is with some of the
alternatives, is a perfectly valid argument in favour of matlab.

You just have to be aware that the choise stands between one
or the other: *Either* the convenient functionality, GUI,
canned immediately useful routines etc, *or* run-time efficiency.

Rune
0
Reply allnor (8474) 2/16/2010 12:36:24 PM

Uwe Brauer <oub@mat.ucm.es> wrote in message <87d405bfk7.fsf@mat.ucm.es>...
> 
> I know about profile but is there any other tool available
> to measure whether the code is efficient. Clock? Tic?
> 
Write the code and make sure its correct. Then run it. If it completes within acceptable time, then its OK (unless ypu have to ship it out to thrid parties who might need bigger datasets).
If it doesn't run in acceptable time, use profile to find out where it is spending its time.

Then you've got to decide whether you can do algorithmic optimisation or not. Usually this involves replacing a simle algorithm with a more complex one that has lower order of runtime. For instance you can find a match in a list of strings by linear search, which is O(N), or you can make sure the list is sorted and do a binary search, which is O(log N).

After you've got the order of ypur algorithm down as far as possible, try removing abstraction. Usually programs spend a not insignificant amunt of time reformatting data so that it is presented in a way that subroutines expect it. Eg one subroutine might want a cell array of strings, another an array of structs with a 'name' field, so you can rewrite the cell array of strings function to operate on the structure directly. This destroys its generality, but eliminates a reformat operation.

If it is still too slow, try micro-optimisation. This involves things like removing unnecessary copies, or storing the strings as 8 bit integers rather than 16 bit characters. Also, in matlab, replacing for loops with implicit vector looping statements. This is the point at which clock timings are useful, because it is not always obvious whether micro-optimisation is in fact working.

The most aggressive form of micro-optimsation is to rewrite the function as a C or Fortran Mex file. This can give 10x speed up. Micro-optimisation doesn't mean 'slight optimisation' but optimisation that doesn't change the operations.
0
Reply malcolm.mclean5 (740) 2/16/2010 12:47:02 PM

6 Replies
22 Views

(page loaded in 0.141 seconds)


Reply: