Hi everyone,
Yesterday I installed VBCC, and today I have been trying to write a program to test the computation speed of a string function I wrote (Just for fun, It's not really a serious program).
To do this I needed to get the time before and after the compututation, first I tried time.h (from the C Standard Library). I found out that with VBCC clock() returns -1 with VBCC. (This is standard compliant but useless for me!).
So I did a google search and found this thread on EAB:
http://eab.abime.net/showpost.php?p=761365&postcount=6I tried the sample by Thomas Rapp, here is my actual code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <devices/timer.h>
#include <proto/timer.h>
#include <clib/timer_protos.h>
struct Library *TimerBase;
static struct timeval startTime;
void startup(){
GetSysTime(&startTime);
}
ULONG getMilliseconds(){
struct timeval endTime;
GetSysTime(&endTime);
SubTime(&endTime,&startTime);
return (endTime.tv_secs * 1000 + endTime.tv_micro / 1000);
}
char* realloc_strcat(char *s1, char * s2){
int len = (strlen(s1) + strlen(s2));
s1 = (char *) realloc(s1, len + 1);
strncat(s1,s2,len);
return s1;
}
int main(int argc, char * argv[]){
// Start timer
startup();
// Initialise string
int i;
char* str;
str = (char *) malloc(7);
strcpy(str,"Hello ");
// Call timer (1)
printf("Time 1: %lu\n",getMilliseconds());
// Computation
for(i=0; i<100; i++){
str = realloc_strcat(str,"Appending string...!)");
}
// Call timer (2)
printf("Time 2: %lu\n",getMilliseconds());
}
I struggled at first to find out what to include! There is no mention of the includes needed to run the code in the EAB thread! And I couldn't find any examples of using timer.device.
It compiles fine, however I got errors about TimerBase, so I added in that code.
I compile with vbcc like so: vc strings.c -o strings -c99 -lmieee
However, when I run the code it crashes straight away! (I get the "Suspend", "Reboot" requester). The crash is caused by calling the startup() function.
Can anyone shed any light on why this may not be working?
Or provide simple examples of using timer.device to get the current milliseconds using C!
Finally, can anyone reccomend a nice Amiga programming guide/book using C for absolute beginners?
Thanks, Alex.