![]() |
![]() |
#1 |
Posts: n/a
|
Using timer.device in C (VBCC)
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/showthread.php?p=761327 I tried the sample by Thomas Rapp, here is my actual code: 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()); } 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 for any help! Alex. |
![]() |
#2 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
You need to open the timer.device using OpenDevice() and then pick the device base from the IORequest structure you pass to OpenDevice() and store this in TimerBase.
If this is your first taste of the easy-to-use AmigaOS APIs then you may throw up in the corner over there ---> EDIT: here's an example for you: Code:
#include <stdio.h> #include <clib/timer_protos.h> #include <clib/exec_protos.h> struct Device* TimerBase; static struct IORequest timereq; static ULONG timer() { static struct timeval t; struct timeval a, b; GetSysTime(&a); b = a; SubTime(&b, &t); t = a; return b.tv_secs*1000 + b.tv_micro/1000; } int main() { OpenDevice("timer.device", 0, &timereq, 0); TimerBase = timereq.io_Device; timer(); for(int n = 200; n--;) printf("%i ", n); ULONG t = timer(); printf("\n%u ms\n", t); CloseDevice(&timereq); return 0; } Last edited by Leffmann; 28 June 2011 at 21:11. |
![]() |
![]() |
#3 | ||
Posts: n/a
|
Quote:
I will try this code tomorrow. Quote:
![]() |
||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Compiling CLib37 with VBCC 0.9 | tnt23 | Coders. General | 2 | 29 July 2013 10:28 |
Old timer, new member ! | cyberknight | Member Introductions | 14 | 23 May 2009 11:07 |
VBCC 0.8j for Windows | hitchhikr | Coders. General | 11 | 09 October 2008 00:58 |
uaehf.device and HDToolbox: Error 224 reading device description | Ebster | support.WinUAE | 3 | 16 September 2008 09:24 |
Kickstart 1.3 and GCC or VBCC? | cdoty | Coders. General | 1 | 23 April 2005 06:10 |
|
|