Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:19:40+00:00 2026-06-08T15:19:40+00:00

I am looking for an iOS analog for Android’s SystemClock.currentThreadTimeMillis() or Microsoft’s GetThreadTimes() or

  • 0

I am looking for an iOS analog for Android’s SystemClock.currentThreadTimeMillis() or Microsoft’s GetThreadTimes() or Posix clock_gettime(CLOCK_THREAD_CPUTIME_ID, ) and pthread_getcpuclockid() functions to measure the actual “clean” time used by a function in a multithreaded application. That is, I don’t want to measure the actual wall clock time spent in a function, but the on-CPU time.

I found interesting discussions about this here on stackoverflow and elsewhere. Unfortunately, neither applies to iOS.

Is there a comparable function for this on iOS?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:19:41+00:00Added an answer on June 8, 2026 at 3:19 pm

    In case anyone is looking for a good answer:

    A while ago I found some great code in this answer (for finding CPU time/memory usage in OSX), and adapted it slightly. I used this for benchmarking some NEON optimizations on the ARM. You would probably only need the section which gets time for the current thread.

    #include <sys/types.h>
    #include <sys/sysctl.h>
    #include <mach/mach_init.h>
    #include <mach/mach_host.h>
    #include <mach/mach_port.h>
    #include <mach/mach_traps.h>
    #include <mach/task_info.h>
    #include <mach/thread_info.h>
    #include <mach/thread_act.h>
    #include <mach/vm_region.h>
    #include <mach/vm_map.h>
    #include <mach/task.h>
    
    
    typedef struct {
        double utime, stime;
    } CPUTime;
    
    int get_cpu_time(CPUTime *rpd, bool_t thread_only)
    {
        task_t task;
        kern_return_t error;
        mach_msg_type_number_t count;
        thread_array_t thread_table;
        thread_basic_info_t thi;
        thread_basic_info_data_t thi_data;
        unsigned table_size;
        struct task_basic_info ti;
    
        if (thread_only) {
            // just get time of this thread
            count = THREAD_BASIC_INFO_COUNT;
            thi = &thi_data;
            error = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)thi, &count);
            rpd->utime = thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
            rpd->stime = thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
            return 0;
        }
    
    
        // get total time of the current process
    
        task = mach_task_self();
        count = TASK_BASIC_INFO_COUNT;
        error = task_info(task, TASK_BASIC_INFO, (task_info_t)&ti, &count);
        assert(error == KERN_SUCCESS);
        { /* calculate CPU times, adapted from top/libtop.c */
            unsigned i;
            // the following times are for threads which have already terminated and gone away
            rpd->utime = ti.user_time.seconds + ti.user_time.microseconds * 1e-6;
            rpd->stime = ti.system_time.seconds + ti.system_time.microseconds * 1e-6;
            error = task_threads(task, &thread_table, &table_size);
            assert(error == KERN_SUCCESS);
            thi = &thi_data;
            // for each active thread, add up thread time
            for (i = 0; i != table_size; ++i) {
                count = THREAD_BASIC_INFO_COUNT;
                error = thread_info(thread_table[i], THREAD_BASIC_INFO, (thread_info_t)thi, &count);
                assert(error == KERN_SUCCESS);
                if ((thi->flags & TH_FLAGS_IDLE) == 0) {
                    rpd->utime += thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
                    rpd->stime += thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
                }
                error = mach_port_deallocate(mach_task_self(), thread_table[i]);
                assert(error == KERN_SUCCESS);
            }
            error = vm_deallocate(mach_task_self(), (vm_offset_t)thread_table, table_size * sizeof(thread_array_t));
            assert(error == KERN_SUCCESS);
        }
        if (task != mach_task_self()) {
            mach_port_deallocate(mach_task_self(), task);
            assert(error == KERN_SUCCESS);
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im looking for a mobile SDK that has the following for iOS and Android
I'm looking for some good testing tools (emulators) for Android and iOS to test
I'm looking to create a iOS app utilizing SIP functionality for various functions. I've
I'm looking to incorporate a video chat function in my android/iOS app, and I
I have created the Android version of an ios app and am looking for
I'm looking for the iOS objective c equivalent to Android's Paint.breakText() . I have
On iOS, I am looking for an API equivalent to the encrypt/decrpyt DPAPI functions
I'm looking to write some Android or iOS applications to access a Bluetooth heart-rate
I am looking for android equivalent of UISwitch(iOS), which I could use to switch
I'm writing a business app for ios and android and looking at my options.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.