lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday 05, Bogdan Marinescu wrote:
> - the memlimit 'a' mode can decrease the required memory space
> dramatically, but it can also increase the execution time
> dramatically. If you're memory constrained and the speed loss is less
> important to you, this is definetely the way to go. But it still has
> some bugs, as can be seen from the table ('factorial.lua' doesn't run
> in 'a' mode). According to Robert, this happens because his patch
> doesn't yet handle closures.
Right now I am having a lot of problems with the upvalues for closures being 
collected when they should be.  Fixing closures will make the patch much more 
complex.

> - the 'f' mode also improves the memory consumption, without having
> much impact on the performance.
> - overall, Robert's patch is a Good Thing if you need the 'a' mode.
One way to improve the performance of 'a' mode is to only all the GC every 10 
or more new allocations, instead of every allocation.

Also I am thinking about trying to use a step collection instead of a full 
collection.  With a step collection the allocator can tell the GC how much 
memory it needs to have freed.  The current patch doesn't protect the step 
collection from recursive calls, so testing this will need to wait until I 
finish fixing closures.

> PS. Anybody knows an easy way to get millisecond-accurate timers with
> Lua under Linux?
GLib has timers that are simple to use.
http://library.gnome.org/devel/glib/unstable/glib-Timers.html

I have attached code for a simple function that wraps "getrusage()" for 
millisecond time resolution.  I use this function when I don't want to link 
in GLib just for a timer.

-- 
Robert G. Jakabosky
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>

#define TIMEVAL_MSECS(tv) ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0))
/**
 * Get user+system time used in seconds (with fractional part in millisecond resolution)
 */
static double get_time_secs() {
  struct rusage res;

  if(getrusage(RUSAGE_SELF,&res) != 0) {
    perror("getrusage");
    return 0;
  }
	return (TIMEVAL_MSECS(res.ru_utime) + TIMEVAL_MSECS(res.ru_stime));
}

static unsigned long fib(unsigned long n)
{
	return( (n < 2) ? 1 : (fib(n-2) + fib(n-1)) );
}

int main(int argc, char *argv[]) {
	double start,end;
	unsigned long f;
	int n=30;

	if(argc > 1) {
		n = atoi(argv[1]);
	}
	start = get_time_secs();
	f = fib(n);
	end = get_time_secs();
	printf("fib(%d)=%lu, time=%.3f\n", n, f, (end - start));
	return 0;
}