Giter Club home page Giter Club logo

cocoa_poll_events_latency's Introduction

Latency of Polling Next Event if Available on MacOS

Question on StackOverflow

# compile
clang -g -ggdb -O2 -framework Cocoa -x objective-c -o main main.m 

# run test
./main

# generate plot using R
R -f plot.R

Description

Simple script measuring the latency of the nextEventMatching call for a custom game loop.

while (num_ticks < max_ticks) {
    // poll events
    @autoreleasepool {
        for (;;) {
            uint64_t t = mach_absolute_time();
            NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny 
                                                untilDate:nil 
                                                   inMode:NSDefaultRunLoopMode
                                                  dequeue:YES]; // same as untilDate:[NSDate distantPast]
            t = mach_absolute_time() - t;
            ticks[num_ticks] = (Tick) { 
                .mach_time_diff = t, 
                .event = (event) ? (uint64_t) [event type] : (uint64_t) 0
            };
            ++num_ticks;
            if (event) {
                [NSApp sendEvent:event];
            } else {
                break;
            }
        }
    }
    usleep(15000); // simulate update and vsynced render: microseconds sleep
}
# event type legend
0   nil                               
1   NSEventTypeLeftMouseDown          
2   NSEventTypeLeftMouseUp            
3   NSEventTypeRightMouseDown         
4   NSEventTypeRightMouseUp           
5   NSEventTypeMouseMoved             
6   NSEventTypeLeftMouseDragged       
7   NSEventTypeRightMouseDragged      
8   NSEventTypeMouseEntered           
9   NSEventTypeMouseExited            
10  NSEventTypeKeyDown                
11  NSEventTypeKeyUp                  
12  NSEventTypeFlagsChanged           
13  NSEventTypeAppKitDefined          
14  NSEventTypeSystemDefined          
15  NSEventTypeApplicationDefined     
16  NSEventTypePeriodic               
17  NSEventTypeCursorUpdate           
22  NSEventTypeScrollWheel            
23  NSEventTypeTabletPoint            
24  NSEventTypeTabletProximity        
25  NSEventTypeOtherMouseDown         
26  NSEventTypeOtherMouseUp           
27  NSEventTypeOtherMouseDragged      

cocoa_poll_events_latency's People

Contributors

laurolins avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.