diff --git a/src/builtin_functions.c b/src/builtin_functions.c
index 4f0019bc9943592be0696f401d6fc52a4ff00d9b..33bbff256ec707eb011ecb5bd82a28f52a2e9209 100644
--- a/src/builtin_functions.c
+++ b/src/builtin_functions.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: builtin_functions.c,v 1.137 1998/10/31 21:31:52 grubba Exp $");
+RCSID("$Id: builtin_functions.c,v 1.138 1998/11/13 01:28:41 hubbe Exp $");
 #include "interpret.h"
 #include "svalue.h"
 #include "pike_macros.h"
@@ -3237,7 +3237,8 @@ static void f_get_prof_info(INT32 args)
 
       push_int(prog->identifiers[i].num_calls);
       push_int(prog->identifiers[i].total_time);
-      f_aggregate(2);
+      push_int(prog->identifiers[i].self_time);
+      f_aggregate(3);
     }
   }
   f_aggregate_mapping(num_functions * 2);
diff --git a/src/interpret.c b/src/interpret.c
index 496cd4252ff3b5b7db7898a6c752486ae8c78745..3b3d5197a3823d6cc80c12a771916f7bec7b0bd6 100644
--- a/src/interpret.c
+++ b/src/interpret.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: interpret.c,v 1.97 1998/11/09 07:23:15 hubbe Exp $");
+RCSID("$Id: interpret.c,v 1.98 1998/11/13 01:28:43 hubbe Exp $");
 #include "interpret.h"
 #include "object.h"
 #include "program.h"
@@ -1788,7 +1788,10 @@ void mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2)
   struct svalue *save_sp=sp-args;
 #ifdef PROFILING
 #ifdef HAVE_GETHRTIME
+  static long long accounted_time =0;
+  long long children_base = accounted_time;
   long long start_time = gethrtime();
+  unsigned INT32 self_time_base;
 #endif
 #endif
 
@@ -1983,6 +1986,13 @@ void mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2)
 	PIKE_ERROR(function->name->str, "Calling undefined function.\n", sp, args);
       
       tailrecurse=-1;
+
+#ifdef PROFILING
+#ifdef HAVE_GETHRTIME
+      self_time_base=function->total_time;
+#endif
+#endif
+
       switch(function->identifier_flags & (IDENTIFIER_FUNCTION | IDENTIFIER_CONSTANT))
       {
       case IDENTIFIER_C_FUNCTION:
@@ -2084,7 +2094,25 @@ void mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2)
       }
 #ifdef PROFILING
 #ifdef HAVE_GETHRTIME
-      function->total_time+=(INT32)((gethrtime()-start_time)/1000);
+      {
+	long long time_passed, time_in_children, self_time;
+	time_in_children=  accounted_time - children_base;
+	time_passed = gethrtime() - start_time;
+	self_time=time_passed - time_in_children;
+	accounted_time+=self_time;
+#ifdef DEBUG
+	if(self_time < 0)
+	  fatal("Self time is negative\n  self_time=%ld\n  time_passed=%ld\n  time_in_children=%ld\n  children_base=%ld\n  accounted_time=%ld!\n",
+		(long)(self_time/1000),
+		(long)(time_passed/1000),
+		(long)(time_in_children/1000),
+		(long)(children_base/1000),
+		(long)(accounted_time/1000)
+		);
+#endif
+	function->total_time=self_time_base + (INT32)(time_passed /1000);
+	function->self_time+=(INT32)( self_time /1000);
+      }
 #endif
 #endif
 
diff --git a/src/program.c b/src/program.c
index 2c680814c74961857cf0a2c2d1c331a6abc186da..131676b8c33c82e7806abee2dfdd65ffb7f4938a 100644
--- a/src/program.c
+++ b/src/program.c
@@ -4,7 +4,7 @@
 ||| See the files COPYING and DISCLAIMER for more information.
 \*/
 #include "global.h"
-RCSID("$Id: program.c,v 1.103 1998/11/09 07:23:16 hubbe Exp $");
+RCSID("$Id: program.c,v 1.104 1998/11/13 01:28:44 hubbe Exp $");
 #include "program.h"
 #include "object.h"
 #include "dynamic_buffer.h"
@@ -1417,6 +1417,7 @@ int low_define_variable(struct pike_string *name,
   dummy.run_time_type=run_time_type;
   dummy.func.offset=offset;
 #ifdef PROFILING
+  dummy.self_time=0;
   dummy.num_calls=0;
   dummy.total_time=0;
 #endif
@@ -1596,6 +1597,7 @@ int add_constant(struct pike_string *name,
   ref.inherit_offset=0;
 
 #ifdef PROFILING
+  dummy.self_time=0;
   dummy.num_calls=0;
   dummy.total_time=0;
 #endif
@@ -1742,6 +1744,7 @@ INT32 define_function(struct pike_string *name,
   INT32 i;
 
 #ifdef PROFILING
+  fun.self_time=0;
   fun.num_calls=0;
   fun.total_time=0;
 #endif
diff --git a/src/program.h b/src/program.h
index 7025dfefa2869674f1241b39db74cf4c7b8ebc02..4fb72e8328d2f6c83416f05385acf4edb6e6592f 100644
--- a/src/program.h
+++ b/src/program.h
@@ -5,7 +5,7 @@
 \*/
 
 /*
- * $Id: program.h,v 1.44 1998/06/24 04:56:46 hubbe Exp $
+ * $Id: program.h,v 1.45 1998/11/13 01:28:45 hubbe Exp $
  */
 #ifndef PROGRAM_H
 #define PROGRAM_H
@@ -124,6 +124,7 @@ struct identifier
 #ifdef PROFILING
   unsigned INT32 num_calls;
   unsigned INT32 total_time;
+  unsigned INT32 self_time;
 #endif /* PROFILING */
   union idptr func;
 };