By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

macro function with iteration

P: n/a
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

Mar 7 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
am******@gmail.com 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.
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.)

--
Chris "electric hedgehog" Dollin
"What I don't understand is this ..." Trevor Chaplin, /The Beiderbeck Affair/

Mar 7 '07 #2

P: n/a
am******@gmail.com wrote On 03/07/07 10:01,:
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?

--
Er*********@sun.com
Mar 7 '07 #3

P: n/a
On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.comwrote:
amit....@gmail.com 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.

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.)

--
Chris "electric hedgehog" Dollin
"What I don't understand is this ..." Trevor Chaplin, /The Beiderbeck Affair/
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

Mar 7 '07 #4

P: n/a
am******@gmail.com 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())

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So....that would make Bethany part black?
Mar 7 '07 #5

P: n/a
am******@gmail.com wrote:
On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.comwrote:
>amit....@gmail.com 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
"""
<snip>
>
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)

--
imalone
Mar 7 '07 #6

P: n/a
am******@gmail.com wrote:
On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.comwrote:
>amit....@gmail.com wrote:
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.

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.

--
Chris "electric hedgehog" Dollin
A rock is not a fact. A rock is a rock.

Mar 7 '07 #7

P: n/a
am******@gmail.com 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Haven't you ever heard the customer is always right?
Mar 7 '07 #8

P: n/a
On Mar 7, 6:24 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.orgwrote:
amit....@gmail.com 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.

--
SM Ryanhttp://www.rawbw.com/~wyrmwif/
Haven't you ever heard the customer is always right?
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.

Mar 7 '07 #9

P: n/a
am******@gmail.com wrote:
On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.comwrote:
amit....@gmail.com 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.
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.)

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.

Mar 7 '07 #10

P: n/a
am******@gmail.com wrote:

<snip>
amit....@gmail.com wrote:
<snip>
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.

Mar 7 '07 #11

P: n/a
am******@gmail.com 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Where do you get those wonderful toys?
Mar 7 '07 #12

P: n/a
Chris Dollin <ch**********@hp.comwrites:
am******@gmail.com 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").

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 7 '07 #13

P: n/a
Keith Thompson wrote:
Chris Dollin <ch**********@hp.comwrites:
>"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").
Brain patched. Thanks, Keith.

--
Chris "electric hedgehog" Dollin
"How am I to understand if you won't teach me?" - Trippa, /Falling/

Mar 8 '07 #14

P: n/a
On Mar 7, 8:03 am, amit....@gmail.com wrote:
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

Mar 8 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.