473,396 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

macro function with iteration

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
14 3023
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
4
by: Jeff Rodriguez | last post by:
Main just loops over this while it's not null. The segfault occurs at this line: *line = (char)ch; Also, please don't just fix the code. I would like to know why exactly this isn't working so I...
7
by: Tobin Fricke | last post by:
I have a wrapper function I use to check the error conditions of various functions: wrap(foo(1,2,3)); (1) while (1 == wrap(bar("fluffy"))) { ... } (2)...
27
by: Ken Human | last post by:
I want to generate every possible 16 character combination of the characters 0-9, A-Z, and a-z programatically. My current code follows: #include <stdio.h> #include <ctype.h> int main() {...
9
by: Jimakos Bilakis | last post by:
Hi everyone! I want to create a function that it will take as parameters a table (float), an integer (the number of Table's rows) and it will re-arrange the elements in the table from the...
4
by: java.koder | last post by:
Hello all, I am making a simple msAccess aplication for some construction calculus, I need however to calculate a parameter from an equation and I am told this can be made with the solver...
4
by: jj6849 | last post by:
I have been using the dom to add a row to my form for awhile now, but now I need to do some validation to make sure certain check boxes aren't checked with other check boxes. Now of course it works...
1
by: aashishn86 | last post by:
i create dynamic rows using this function : function addRow() { var tbl = document.getElementById('applications'); var lastRow = tbl.rows.length; var iteration = lastRow; var row =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.