Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Logging number of computations and memory ussage

0 views
Skip to first unread message

Jasper

unread,
Mar 11, 2010, 2:41:59 PM3/11/10
to
Hi

I have written 3 matlab programs doing almost the same thing but in
different ways. Is it possible to measure how much memory is used and
the number of computations each of the programs takes to do th task?

Regards,
Jasper

jason

unread,
Mar 11, 2010, 2:49:14 PM3/11/10
to

you can use the built in profiler

and try
help memory

Walter Roberson

unread,
Mar 11, 2010, 3:21:42 PM3/11/10
to
jason wrote:
> On Mar 11, 2:41 pm, Jasper <junkmail...@gmail.com> wrote:

>> I have written 3 matlab programs doing almost the same thing but in
>> different ways. Is it possible to measure how much memory is used and
>> the number of computations each of the programs takes to do th task?

> you can use the built in profiler

The built-in profiler measures the amount of time spent on various lines, but
as best I recall, it does not measure the number of computations the program
takes to do the task.


"the number of computations to do a task" is difficult to give a precise and
useful meaning to on any modern CPU architecture that has multiple subsystems
and can compute several results simultaneously. Modern compilers take
advantage of the knowledge of what can or cannot be overlapped, and if the
circumstances warrant, will sometimes deliberately compile a single numeric
operation into multiple instructions, knowing that those multiple instructions
can be executed at the same time as something else is happening. For
example, if there are two double-precision floating point multiplications to
be done, the compiler might compile one of them as a plain double precision
multiplication using the floating point unit, but might compile the other one
in terms of ANDs and ORs and several integer half-multiplications (software
double precision multiplication) thus taking advantage of integer unit when it
would otherwise be busy. How can you meaningfully count "computations" in such
a case?


Another computation trick available on some architectures, has to do with
flows such as

if some_condition
result = calculation_1
else
result = calculation_2
end


Processing "if" conditions is relatively slow because of the implicit GOTO's
involved, one branch at the test if the condition is false, and the other
branch at the end of calculation_1, to skip around calculation_2 : GOTO
requires flushing the instruction cache, and in some architectures require
that all operations that are simultaneously going on be completed before the
branch is executed, so that exception conditions are ascribed to the right
place in the code and use the proper exception handler.

To get around this branching bottleneck, some architectures will calculate
_both_ calculation_1 and calculation_2, separately queuing up any exceptions
encountered along the way, and then when they have finished both calculations,
they have a "MOVE CONDITIONAL" instruction that tests the condition, moves the
appropriate calculated result to its destination according which branch would
have been chosen, discards the exception queue for the branch not chosen, and
processes the pending exception queue associated with the branch that was
chosen. It makes the processor architecture more complex, but it _does_ make
noticeable processing-speed differences, especially if you have multiple cores
or the equivalent. But how do you count the "computations" in such a case --
do you count the computations whose results ended up being discarded?


There used to be a flops() function in Matlab, but as instruction
architectures became more complex, the function became semantically meaningless.

Jasper

unread,
Mar 11, 2010, 4:19:30 PM3/11/10
to
Hi

Thank you very much for the deep explanation. I can see now that it is
not as easy as i thought. But how would you then examine which code is
the most effective? Because measuring the time of execution will also
depend on other operations of the CPU, and not just my piece of code?
Is there a general way to determine the quality of some code then?

Thanks again for the answers!

Regards, Jasper.

Steven Lord

unread,
Mar 11, 2010, 5:30:35 PM3/11/10
to

"Jasper" <junkm...@gmail.com> wrote in message
news:020d3c1b-e41b-44bc...@z11g2000yqz.googlegroups.com...

> Hi
>
> Thank you very much for the deep explanation. I can see now that it is
> not as easy as i thought. But how would you then examine which code is
> the most effective?

That depends on how you define "effective". If you have a machine with very
little memory, but you can start the program running when you leave for the
night and get the results when you return in the morning, "effective" would
mean "memory efficient". On the other hand, if you need results RIGHT NOW
but you have a machine with 1 TB of RAM, "effective" means "low computation
time".

> Because measuring the time of execution will also
> depend on other operations of the CPU, and not just my piece of code?

It could depend on:

how much memory is on your machine
what kind of graphics card you're using (if your code displays graphics)
what other applications are running on your machine
how you've written your code
the data on which you're operating
http://en.wikipedia.org/wiki/Denormal
the order in which you perform your operations
and many other factors

> Is there a general way to determine the quality of some code then?

No, because that depends on how you define "quality".

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


0 new messages