GetTickCount on the mac

1,731 views
Skip to first unread message

Christopher Khaljah Xu

unread,
Jan 30, 2009, 9:27:21 AM1/30/09
to darwi...@lists.apple.com
Hi there!
I'm a rookie on a mac. I've been trying to port some windows programs
that was written in C/C++ onto the mac.
I need to rewrite a timer that measures CPU ticks between the start
and end of an operation. I wonder if there is something in the mac api
similar as GetTickCount or GetTickCount64 ?

Any help will be appreciated.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (Darwi...@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/darwin-dev-garchive-73044%40googlegroups.com

This email sent to darwin-dev-g...@googlegroups.com

Jason Coco

unread,
Jan 30, 2009, 10:05:27 AM1/30/09
to Christopher Khaljah Xu, darwi...@lists.apple.com

On Jan 30, 2009, at 09:27 , Christopher Khaljah Xu wrote:

> Hi there!
> I'm a rookie on a mac. I've been trying to port some windows
> programs that was written in C/C++ onto the mac.
> I need to rewrite a timer that measures CPU ticks between the start
> and end of an operation. I wonder if there is something in the mac
> api similar as GetTickCount or GetTickCount64 ?

You can use mach_absolute_time() (declared in mach/mach_time.h) for
this purpose. For more information on using this routine, see this
technical qa: http://developer.apple.com/qa/qa2004/qa1398.html

Jason

Andy Wiese

unread,
Jan 30, 2009, 4:01:52 PM1/30/09
to development Darwin list

On Jan 30, 2009, at 9:05 AM, Jason Coco wrote:

>
> On Jan 30, 2009, at 09:27 , Christopher Khaljah Xu wrote:
>
>> Hi there!
>> I'm a rookie on a mac. I've been trying to port some windows
>> programs that was written in C/C++ onto the mac.
>> I need to rewrite a timer that measures CPU ticks between the start
>> and end of an operation. I wonder if there is something in the mac
>> api similar as GetTickCount or GetTickCount64 ?
>
> You can use mach_absolute_time() (declared in mach/mach_time.h) for
> this purpose. For more information on using this routine, see this
> technical qa: http://developer.apple.com/qa/qa2004/qa1398.html
>
> Jason


How does clock(3) compare to mach_absolute_time()?

Jason Coco

unread,
Jan 30, 2009, 4:22:22 PM1/30/09
to Andy Wiese, development Darwin list

On Jan 30, 2009, at 16:01 , Andy Wiese wrote:

>
> On Jan 30, 2009, at 9:05 AM, Jason Coco wrote:
>
>>
>> On Jan 30, 2009, at 09:27 , Christopher Khaljah Xu wrote:
>>
>>> Hi there!
>>> I'm a rookie on a mac. I've been trying to port some windows
>>> programs that was written in C/C++ onto the mac.
>>> I need to rewrite a timer that measures CPU ticks between the
>>> start and end of an operation. I wonder if there is something in
>>> the mac api similar as GetTickCount or GetTickCount64 ?
>>
>> You can use mach_absolute_time() (declared in mach/mach_time.h) for
>> this purpose. For more information on using this routine, see this
>> technical qa: http://developer.apple.com/qa/qa2004/qa1398.html
>>
>> Jason
>
>
> How does clock(3) compare to mach_absolute_time()?

mach_absolute_time() actually counts the clock cycles and is the
reference timer for everything else. Internally, from the C library,
clock(3) calls getrusage(2) on itself and returns the converted sum of
its system and user time.

Shawn Erickson

unread,
Jan 30, 2009, 4:23:45 PM1/30/09
to Andy Wiese, development Darwin list
On Fri, Jan 30, 2009 at 1:01 PM, Andy Wiese <an...@mitosystems.com> wrote:

> How does clock(3) compare to mach_absolute_time()?

Poorly... given that the resolution of clock is 100 ticks per second
on Mac OS X while mach_absolute_time can get into the 10s / 100s of
nanoseconds.

-Shawn

Tommy Nordgren

unread,
Jan 30, 2009, 4:32:00 PM1/30/09
to Christopher Khaljah Xu, darwi...@lists.apple.com

On Jan 30, 2009, at 3:27 PM, Christopher Khaljah Xu wrote:

> Hi there!
> I'm a rookie on a mac. I've been trying to port some windows
> programs that was written in C/C++ onto the mac.
> I need to rewrite a timer that measures CPU ticks between the start
> and end of an operation. I wonder if there is something in the mac
> api similar as GetTickCount or GetTickCount64 ?
>

#include <CoreServices/CoreServices.h>

long TickCount();

//Found in ca one minute by searching the Xcode docs for for symbols
containing 'TickCount'
------------------------------------------------------
"Home is not where you are born, but where your heart finds peace" -
Tommy Nordgren, "The dying old crone"
tommy.n...@comhem.se

Andy Wiese

unread,
Jan 30, 2009, 5:09:50 PM1/30/09
to development Darwin list

On Jan 30, 2009, at 3:22 PM, Jason Coco wrote:

>
> On Jan 30, 2009, at 16:01 , Andy Wiese wrote:
>
>>
>> On Jan 30, 2009, at 9:05 AM, Jason Coco wrote:
>>
>>>
>>> On Jan 30, 2009, at 09:27 , Christopher Khaljah Xu wrote:
>>>
>>>> Hi there!
>>>> I'm a rookie on a mac. I've been trying to port some windows
>>>> programs that was written in C/C++ onto the mac.
>>>> I need to rewrite a timer that measures CPU ticks between the
>>>> start and end of an operation. I wonder if there is something in
>>>> the mac api similar as GetTickCount or GetTickCount64 ?
>>>
>>> You can use mach_absolute_time() (declared in mach/mach_time.h)
>>> for this purpose. For more information on using this routine, see
>>> this technical qa: http://developer.apple.com/qa/qa2004/qa1398.html
>>>
>>> Jason
>>
>>
>> How does clock(3) compare to mach_absolute_time()?
>
> mach_absolute_time() actually counts the clock cycles and is the
> reference timer for everything else. Internally, from the C library,
> clock(3) calls getrusage(2) on itself and returns the converted sum
> of its system and user time.

So resolution issues aside (I understand that clock is low
resolution), clock(3) attempts to measure the cpu usage of the
particular process, and mach_absolute_time will give the system global
cpu time. Correct?

So for my purposes, I want rough measurements of a single relatively
long-running task (a cgi handler) in an environment that is polluted
with many such processes, so clock(3) is actually a pretty good
choice. (The fact that the deployment environment is FreeBSD makes it
an even better choice, but that is off-topic and off-platform).

Reply all
Reply to author
Forward
0 new messages