Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macro function with iteration

Reply
Thread Tools

macro function with iteration

 
 
amit.man@gmail.com
Guest
Posts: n/a
 
      03-07-2007
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

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      03-07-2007
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/

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-07-2007
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?

--

 
Reply With Quote
 
amit.man@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.com> wrote:
> 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

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      03-07-2007
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?
 
Reply With Quote
 
Ian Malone
Guest
Posts: n/a
 
      03-07-2007
wrote:
> On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.com> wrote:
>> 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
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      03-07-2007
wrote:

> On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.com> wrote:
>> 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.

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      03-07-2007
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?
 
Reply With Quote
 
amit.man@gmail.com
Guest
Posts: n/a
 
      03-07-2007
On Mar 7, 6:24 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.org> wrote:
> 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-07-2007
wrote:
> On Mar 7, 5:40 pm, Chris Dollin <chris.dol...@hp.com> wrote:
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Simple iteration in a function problem Blake Miller Ruby 24 12-02-2006 06:24 PM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57