macro function with iteration

A

amit.man

Hello

i need to write a MACRO function that look something like this

"""
do_somthing();
for(int i=0;i<100;i++) {do_something_else())}
return(somthing_other_then_those()) // a double type
"""

i know this is usauly not recmended to be carried out by a macro, but
for my needs i need it to be.

is it possible to create a macro function that do that? how?

thank you
amit man
 
C

Chris Dollin

Hello

i need to write a MACRO function that look something like this

"""
do_somthing();
for(int i=0;i<100;i++) {do_something_else())}
return(somthing_other_then_those()) // a double type
"""

i know this is usauly not recmended to be carried out by a macro, but
for my needs i need it to be.

Why? Be specific.
is it possible to create a macro function that do that? how?

"macro". Not "macro function". (Although "function macro" is, I
think, the term for a macro that looks like a function call and
is supposed to behave like one.)
 
E

Eric Sosman

Hello

i need to write a MACRO function that look something like this

"""
do_somthing();
for(int i=0;i<100;i++) {do_something_else())}
return(somthing_other_then_those()) // a double type
"""

i know this is usauly not recmended to be carried out by a macro, but
for my needs i need it to be.

is it possible to create a macro function that do that? how?

#define AMIT \
do_something(); \
for(int i=0;i<100;i++) {do_something_else())} \
return(somthing_other_then_those())

In other words, what are you trying to accomplish?
 
A

amit.man

Why? Be specific.


"macro". Not "macro function". (Although "function macro" is, I
think, the term for a macro that looks like a function call and
is supposed to behave like one.)

Hi Chris, thanks for replaying.

i have an assignment in which i need to count CPU cycles for different
tasks (i use some lib for that). since the tasks are very small i
check the amount of time it takes to carry a large number of
iterations of each task.

since i want to exclude form the counting, the time consume by the
iteration process itself (the "for" loop), and i need to do this for
each many different tested tasks, i want to create a macro that -
register inital cycle count, run a empty loop, check the current cycle
count and return the difference.
I want to use a macro and no a function, because calling for a
function seems effect the amount of cycles the computer uses.

amit
 
S

SM Ryan

(e-mail address removed) wrote:
# Hello
#
# i need to write a MACRO function that look something like this
#
# """
# do_somthing();
# for(int i=0;i<100;i++) {do_something_else())}
# return(somthing_other_then_those()) // a double type
# """
#
# i know this is usauly not recmended to be carried out by a macro, but
# for my needs i need it to be.

#define macro \
do_somthing(); \
for(int i=0;i<100;i++) {do_something_else())} \
return(somthing_other_then_those())
 
I

Ian Malone

i have an assignment in which i need to count CPU cycles for different
tasks (i use some lib for that). since the tasks are very small i
check the amount of time it takes to carry a large number of
iterations of each task.

since i want to exclude form the counting, the time consume by the
iteration process itself (the "for" loop), and i need to do this for
each many different tested tasks, i want to create a macro that -
register inital cycle count, run a empty loop, check the current cycle
count and return the difference.
I want to use a macro and no a function, because calling for a
function seems effect the amount of cycles the computer uses.

Write a program to write the code?
(Aside: you have no guarantee the compiler isn't going to
do some optimisation that produces the same code as a for
loop anyway)
 
C

Chris Dollin

Hi Chris, thanks for replaying.

(fx:amuse (reason `typo`))
i have an assignment in which i need to count CPU cycles for different
tasks (i use some lib for that). since the tasks are very small i
check the amount of time it takes to carry a large number of
iterations of each task.

since i want to exclude form the counting, the time consume by the
iteration process itself (the "for" loop), and i need to do this for
each many different tested tasks, i want to create a macro that -
register inital cycle count, run a empty loop, check the current cycle
count and return the difference.

Well ... I think you're on a loser, here, because you're fighting
all of (a) the vageries of how you express your tasks in C (b) the
power (or not) of your compiler's optimisation options and settings
(c) your machine's particular mix of cache and workload and (d) the
phase of the moon. But Assuming that can all be fixed, you can compare
the timings of

for (... 100 times ...) {X}
and
for (... 100 times ...) {X; X}

Their difference will be 100 * timeof(X). Ish. Under the Assumption.

How reasonable the Assumption is ... oh, look, is that the time?
I want to use a macro and no a function, because calling for a
function seems effect the amount of cycles the computer uses.

It may well. But you can use the same trick, if it works, to
subtract away the overhead.

[1] or two or three.
 
S

SM Ryan

(e-mail address removed) wrote:

# since i want to exclude form the counting, the time consume by the
# iteration process itself (the "for" loop), and i need to do this for
# each many different tested tasks, i want to create a macro that -
# register inital cycle count, run a empty loop, check the current cycle
# count and return the difference.
# I want to use a macro and no a function, because calling for a
# function seems effect the amount of cycles the computer uses.

A define simply inserts text into the program without regard
to whether it a statement, an expression, or even C code.
So one thing you could do is
#define intervalStart(n) \
TimeInterval interval; \
{ \
int repeated = (n), repeater; \
Time starttime = clock(); \
for (repeater=1; repeater<=repeated; repeater++) {
#define intervalStop \
} \
Time stoptime = clock(); \
interval = (stoptime-starttime)/repeated; \
}

And then do something like
intervalStart(100)
timed operation
intervalStop
printf("per iteration time = " TimeIntervalFormat "\n",interval);

And of course, many variation on the theme.
 
A

amit.man

(e-mail address removed) wrote:

# since i want to exclude form the counting, the time consume by the
# iteration process itself (the "for" loop), and i need to do this for
# each many different tested tasks, i want to create a macro that -
# register inital cycle count, run a empty loop, check the current cycle
# count and return the difference.
# I want to use a macro and no a function, because calling for a
# function seems effect the amount of cycles the computer uses.

A define simply inserts text into the program without regard
to whether it a statement, an expression, or even C code.
So one thing you could do is
#define intervalStart(n) \
TimeInterval interval; \
{ \
int repeated = (n), repeater; \
Time starttime = clock(); \
for (repeater=1; repeater<=repeated; repeater++) {
#define intervalStop \
} \
Time stoptime = clock(); \
interval = (stoptime-starttime)/repeated; \
}

And then do something like
intervalStart(100)
timed operation
intervalStop
printf("per iteration time = " TimeIntervalFormat "\n",interval);

And of course, many variation on the theme.

I thought about something along this line, but it seems to me that
writing a a macro that is not "self contained" and relay on me
addressing a specific variable "interval" is not a good idea. i am not
sure if it is possible but i would like the macro to return a double
type, not store it in a variable.

P.S - Chris
you are right, surly . it is a school assignment, nothing more :) . is
should show me that s system calls are very expansive. not much more.
but i get graded so i should teat it seriously.
 
S

santosh

Hi Chris, thanks for replaying.

i have an assignment in which i need to count CPU cycles for different
tasks (i use some lib for that). since the tasks are very small i
check the amount of time it takes to carry a large number of
iterations of each task.

Hope you're not doing this in an heavily multitasked environment.
since i want to exclude form the counting, the time consume by the
iteration process itself (the "for" loop), and i need to do this for
each many different tested tasks, i want to create a macro that -
register inital cycle count, run a empty loop, check the current cycle
count and return the difference.
I want to use a macro and no a function, because calling for a
function seems effect the amount of cycles the computer uses.

The time taken by the logic of the looping process is very likely
trivial enough to be swamped by your actual task. Even a function call
is likely slower than a simple jump, (which is what a loop is going to
translate into), a system call will be *far* slower. Even for millions
of iterations the difference is unlikely to matter.
 
S

santosh

(e-mail address removed) wrote:


P.S - Chris
you are right, surly . it is a school assignment, nothing more :) . is
should show me that s system calls are very expansive. not much more.
but i get graded so i should teat it seriously.

Then you can compare a series of system calls with a set of normal
function calls. The purpose of demonstrating the relative latency of a
system call is to show that expensive nature of context switches under
most architectures. Comparison with normal functions should be
sufficient for this. Of course, you most likely need not lose hair
over the "overhead" of a loop logic.
 
S

SM Ryan

(e-mail address removed) wrote:

# I thought about something along this line, but it seems to me that
# writing a a macro that is not "self contained" and relay on me
# addressing a specific variable "interval" is not a good idea. i am not
# sure if it is possible but i would like the macro to return a double
# type, not store it in a variable.

You cannot put an iterative statement inside an expression in
ANSI C. You can do that in some extensions. For example in
gcc you can wrap it

#define intervalStart ({...
#define intervalStop ...; interval;})

TimeInterval xyzzy = intervalStart ... intervalStop;

However this will not run on all C compilers.
 
K

Keith Thompson

Chris Dollin said:
(e-mail address removed) wrote: [...]
is it possible to create a macro function that do that? how?

"macro". Not "macro function". (Although "function macro" is, I
think, the term for a macro that looks like a function call and
is supposed to behave like one.)

The term is "function-like macro" (as opposed to an "object-like
macro").
 
B

bluejack

i have an assignment in which i need to count CPU cycles for different
tasks (i use some lib for that). since the tasks are very small i
check the amount of time it takes to carry a large number of
iterations of each task.

Are you looking for microseconds or cpu cycles? Whagt does your lib
count, and how does it do it?

Other options include: measuring the time of 10,000 empty loops and
subtracting that from your 10,000 task loops; cut & pasting your
function call 10,000 times into a file (not as crazy as it sounds --
cut-n-paste 5 times, then cut the 5 to 10, & to 20, etc.); taking
measurements *inside* the loop (only works if the measurements are
extremely accurate, or the task is long enough that the margin of
error is not significant).

However, usually in these sorts of things what is interesting is the
*relative* benchmarking of different tasks, in which case the cost of
the loop is fixed for all tasks (& as others have mentioned,
vanishingly small compared to your actual work done) -- ergo, not
relevant to your measurement.

-bluejack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Why is each iteration accumulating the values here? 0
Function for primes 0
C function macro question 5
macro chain 4
Help with macro 2
Macro function syntax 11
MACRO help 23
Order of preprocessor macro replacement 3

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top