Page 3 of 4

Posted: Sat Aug 16, 2003 8:02 pm
by quantus
Jason wrote:
Dwindlehop wrote:
quantus wrote:courses? what are those?
you don't have any left?
maybe that's why he keeps failing his PhD qualifier.
To keep failing requires that I fail multiple times which is not the case. Besides, I don't see you trying to take them.

Posted: Tue Oct 07, 2003 9:55 pm
by Jonathan
Peijen wrote:
Dwindlehop wrote:Fuck that noise. There is no testNumEq in numeric_limits. As far as I can tell, the code listing only exists in the print version of that article and isn't available online.
he did outline what you need to do. If you want to wait I will code it up next week, and I will send you a copy since I will need to use it too.

Or you can code it up and send me a copy.
Did you ever write anything like this?

Posted: Wed Oct 08, 2003 1:46 pm
by Peijen
dont think so, I have been assign another project and they told me to put the first one on hold until I finish the new one.

Posted: Wed Oct 08, 2003 6:12 pm
by quantus
heh, don't get too attached to this project either. Chances are they'll tell you to put this one on hold for something else. Iterate 'til you have infinite projects and nothing finished.

The only chance you have of finishing anything is if you get an intern :wink:

Posted: Wed Oct 08, 2003 6:21 pm
by Peijen
quantus wrote:heh, don't get too attached to this project either. Chances are they'll tell you to put this one on hold for something else. Iterate 'til you have infinite projects and nothing finished.

The only chance you have of finishing anything is if you get an intern :wink:
hehe, too bad it's not intern season.

Posted: Tue Oct 14, 2003 6:30 pm
by Jonathan
Argh. Trying to recompile code under libstdc++-v3 that was originally written with libstdc++-v2. What the hell are the non-deprecated io header files now? I can't fuckin' figure it out.

Posted: Tue Oct 14, 2003 6:38 pm
by Jonathan
http://annwm.lbl.gov/~leggett/Atlas/gcc-3.2.html

Going to try the steps detailed here.

Posted: Tue Oct 14, 2003 7:08 pm
by Jonathan
Well, that worked. Now I gotta figure out the differences between <fstream> and <fstream.h> because these deprecated messages are annoying the hell out of me.

Posted: Tue Oct 14, 2003 7:12 pm
by Peijen
Dwindlehop wrote:Well, that worked. Now I gotta figure out the differences between <fstream> and <fstream.h> because these deprecated messages are annoying the hell out of me.
let me know when you do.

standardization is wonderful until it breaks your code!!

Posted: Wed Oct 22, 2003 7:17 pm
by VLSmooth
Don't know if it should go in this thread, but why not.

You can pass functions as parameters in C++ !!!

example:
http://www.csm.astate.edu/~rossa/cs3543/fparm.html

I was thinking about using this for a timing wrapper (evaluates how long an arbitrary method takes). However, since it requires explicitly stating the method signature, and the methods I'm evaluating have different signatures, I'm SOL (shit out of luck).

Sigh, time for repetitive code I guess.

Posted: Wed Oct 22, 2003 7:39 pm
by VLSmooth
Ok, I've just been told this isn't a big deal (and will shortly be on the way to borrow a Stroustrup book). Anyone know off the top of their head how to pass an arbitrary method and parameters to another method while I search?

Posted: Wed Oct 22, 2003 7:48 pm
by Peijen
fn(arg1, arg2, ...)

or something like that, ... tells it there are more argument but we dont know how many now.

Posted: Wed Oct 22, 2003 7:52 pm
by Peijen
ok, no "," before the "..." so

fn(arg1 ...) or fn(arg1, arg2 ...) etc

add number of argument as required

Posted: Wed Oct 22, 2003 8:06 pm
by quantus
Isn't there an include statement too, like xargs.h or something?

Posted: Wed Oct 22, 2003 8:13 pm
by Jonathan
Another method is to specify a void * as the argument to your function. Then, if you know how to decide what to cast the pointer to, you can pass any damn thing you want.

Posted: Wed Oct 22, 2003 8:58 pm
by Peijen
quantus wrote:Isn't there an include statement too, like xargs.h or something?
<cstdarg>

but he only asked about how to do it, not how to use it 8)

Posted: Wed Oct 22, 2003 11:17 pm
by VLSmooth
Dwindlehop wrote:Another method is to specify a void * as the argument to your function. Then, if you know how to decide what to cast the pointer to, you can pass any damn thing you want.
This shows promise, although an example would be even better 8)

Timing Fun

Posted: Wed Oct 22, 2003 11:26 pm
by VLSmooth
Okay, I'm missing something basic here. For some reason, the duration is exactly 0, and I definitely notice the sleep delay.
Note1: sleeptime_variable() is to test passing a method with a different signature in the future.
Note2: not sure the CLOCKS_PER_SEC is set correctly

funct_passing.cpp:

Code: Select all

#include <stdio.h>


void sleeptime() {
  printf("  sleeptime(): start\n");
  sleep(5);
  printf("  sleeptime(): done!\n");
}

void sleeptime_variable(int i) {
  printf("  sleeptime_variable(%d): start\n");
  sleep(i);
  printf("  sleeptime_variable(%d): done!\n");
}

void timer2(void f()) {
  printf("\ntimer2(): start\n");
  clock_t start = clock();
  f();
  printf("timer2(): done! (%lf ms)\n",
	 (clock() - start)/(CLOCKS_PER_SEC / (double) 1000.0));
}

void timer(void f()) {
  printf("\ntimer(): start\n");
  clock_t start = clock();
  f();
  clock_t end = clock();
  printf("timer(): done! (%lf ms)\nNote: ", end-start);
  if (start == end) {
    printf("start == end\n");
  } else {
    printf("start != end\n");
  }
}

void main() {
  timer(sleeptime);
  timer2(sleeptime);
}
Here's the output:

Code: Select all

timer(): start
  sleeptime(): start
  sleeptime(): done!
timer(): done! (0.000000 ms)
Note: start == end

timer2(): start
  sleeptime(): start
  sleeptime(): done!
timer2(): done! (0.000000 ms)

Posted: Wed Oct 22, 2003 11:27 pm
by VLSmooth
On the other hand, this works: (I wrote this earlier for fun). This explains why I'm confused about funct_passing.cpp above. Note that I use a very similar timing method above (just changed the signature), in addition to a new one that differs by a start,end comparison.

vector_speed.cpp:

Code: Select all

#include <stdio.h>



// this is a wrapper that takes in a method
// note: the method's signature must be completely specified
//       in the wrapper's signature
void timer(vector<int> v, void print_method(vector<int>)) {
  clock_t start = clock();
  print_method(v);
  printf("time: %lf ms\n",
	 (clock() - start)/(CLOCKS_PER_SEC / (double) 1000.0));
}

void print_vector_slow(vector<int> v) {
  for(int i=0;i<v.size();i++) {
    //    printf(" %d", v[i]);
  }
  printf("\ndone! (print_vector_slow)\n");
}

void print_vector_medium(vector<int> v) {
  vector<int>::iterator v_iter = v.begin();
  while(v_iter != v.end()) {
    //    printf(" %d", *(v_iter++));
    v_iter++;
  }
  printf("\ndone! (print_vector_medium)\n");
}

void print_vector_fast(vector<int> v) {
  vector<int>::iterator v_iter = v.begin();
  int * v_end = v.end();
  while(v_iter != v_end) {
    //    printf(" %d", *(v_iter++));
    v_iter++;
  }
  printf("\ndone! (print_vector_fast)\n");
}

void main() {
  vector<int> v_x;
  int i = 0; while (i<5000000) { v_x.push_back(i++); }
  timer(v_x,print_vector_slow);
  timer(v_x,print_vector_medium);
  timer(v_x,print_vector_fast);
}
Here's the output:

Code: Select all

done! (print_vector_slow)
time: 380.000000 ms

done! (print_vector_medium)
time: 150.000000 ms

done! (print_vector_fast)
time: 110.000000 ms

Posted: Wed Oct 22, 2003 11:34 pm
by Jonathan

Code: Select all

struct {
  int A;
  int B;
  double C;
} arg1_t;

struct {
  char X;
  char Y;
} arg2_t;

void fn1(void * args) {
   dostuff((arg1_t*)args);
}

void fn2(void * args) {
   dodifferentthings((arg2_t*)args);
}

void callmyfunction(void genericfn(void * args), void * thisargs, SomeType otherarg) {
  calculatestuff(otherarg);
  genericfn(thisargs);
}

int main() {
  arg1_t *penis = initialize();
  callmyfunction(fn1, (void *)penis, iamtirednow)
  return LOVE_FOR_VINNY;
}
You get the idea.