Skip to content

Instantly share code, notes, and snippets.

@adamkaplan
Last active February 8, 2019 18:24
Show Gist options
  • Save adamkaplan/3092660b4e4e8ac742c4e64765cffe07 to your computer and use it in GitHub Desktop.
Save adamkaplan/3092660b4e4e8ac742c4e64765cffe07 to your computer and use it in GitHub Desktop.
#include <mach/mach_time.h>
NSURLSessionDataTask *task;
void(^doFetch)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error);
NSArray *smallImages = @[@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/1342c899c7055c754f17a7e2d948330a.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/3c549674c2851a410348412ea45c2eab.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/85f1c40fe33d2234ec7d5c8bf01e3a25.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/eaa3b450578344d79f1a5ebec9dfdb70.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/5ae2299c586d15ebf5f60aedad7d33d4.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/405c773c257d5ea355770072b5313af5.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/81ace631a5758e0c490f646fc3cd44f3.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/d96c656a7563a84548ee45762c784fa8.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/4d858714aa7e3a1ac9a5a988f51de980.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/494252e3d76cea118ed86d36935e04e6.jpeg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE/82x82/aea8156af239bb48f7d2c75302ec5eb4.jpeg" ];
NSArray *largeImages = @[@"https://s.yimg.com/av/ads/1545403043282-3031.jpg",
@"https://s.yimg.com/av/moneyball/ads/1537303962162-905.jpg",
@"https://s.yimg.com/av/ads/1540577377942-3943.jpg",
@"https://s.yimg.com/av/curveball/ads/pr/RESIZE_AND_CROP/1200x627/0ec741289ee49bddddfcfdbb999b7d1e.jpeg",
@"https://s.yimg.com/av/ads/1538422260449-1369.jpg",
@"https://s.yimg.com/av/ads/1542120824094-4791.jpg",
@"https://s.yimg.com/av/ads/1547570657054-8519.jpg",
@"https://s.yimg.com/av/moneyball/ads/1523575891800-8937.jpg",
@"https://s.yimg.com/av/ads/1547686806587-9359.jpg",
@"https://s.yimg.com/av/ads/1548698030659-3637.jpg",
@"https://s.yimg.com/av/ads/1548779162162-109.jpg"];
__block NSEnumerator *iter;
__block BOOL second = NO;
mach_timebase_info_data_t info;
mach_timebase_info(&info);
__block uint64_t start = 0;
__block uint64_t accum = 0;
doFetch = ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (start != 0) {
uint64_t duration = mach_absolute_time() - start;
duration *= info.numer;
duration /= info.denom;
accum += duration;
NSLog(@"Downloaded image: %ld B in %lld nanoseconds", data.length, duration);
}
NSString *nextStr = iter.nextObject;
task = nil;
if (nextStr == nil) {
if (!second) {
NSLog(@"Small images: %lld nanoseconds", accum);
second = YES;
accum = 0;
start = 0;
iter = largeImages.objectEnumerator;
doFetch(nil, nil, nil);
return;
} else {
NSLog(@"Large images: %lld nanoseconds", accum);
return;
}
}
task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:nextStr] completionHandler:doFetch];
start = mach_absolute_time();
[task resume];
};
accum = 0;
iter = smallImages.objectEnumerator;
doFetch(nil, nil, nil);

Large

Small

2019-02-08 13:01:14.455614-0500 Y! Finance[2610:591283] Downloaded image: 2608 B in 136448958 nanoseconds
2019-02-08 13:01:14.573298-0500 Y! Finance[2610:591203] Downloaded image: 2635 B in 116160666 nanoseconds
2019-02-08 13:01:14.663052-0500 Y! Finance[2610:591225] Downloaded image: 2488 B in 88794083 nanoseconds
2019-02-08 13:01:14.724896-0500 Y! Finance[2610:591201] Downloaded image: 1832 B in 61348500 nanoseconds
2019-02-08 13:01:14.783993-0500 Y! Finance[2610:591191] Downloaded image: 2535 B in 58198333 nanoseconds
2019-02-08 13:01:14.880370-0500 Y! Finance[2610:591191] Downloaded image: 2466 B in 95969333 nanoseconds
2019-02-08 13:01:14.941207-0500 Y! Finance[2610:591197] Downloaded image: 3014 B in 60028333 nanoseconds
2019-02-08 13:01:14.993907-0500 Y! Finance[2610:591197] Downloaded image: 1596 B in 52034625 nanoseconds
2019-02-08 13:01:15.061023-0500 Y! Finance[2610:591191] Downloaded image: 2371 B in 66615791 nanoseconds
2019-02-08 13:01:15.126403-0500 Y! Finance[2610:591197] Downloaded image: 1764 B in 64767375 nanoseconds
2019-02-08 13:01:15.198612-0500 Y! Finance[2610:591201] Downloaded image: 2459 B in 70920875 nanoseconds
2019-02-08 13:01:15.198834-0500 Y! Finance[2610:591201] Small images: 871286872 nanoseconds
2019-02-08 13:01:15.384564-0500 Y! Finance[2610:591201] Downloaded image: 188022 B in 183961125 nanoseconds
2019-02-08 13:01:15.520534-0500 Y! Finance[2610:591206] Downloaded image: 159475 B in 134490791 nanoseconds
2019-02-08 13:01:15.637898-0500 Y! Finance[2610:591201] Downloaded image: 127945 B in 116211416 nanoseconds
2019-02-08 13:01:15.757132-0500 Y! Finance[2610:591197] Downloaded image: 81860 B in 118176500 nanoseconds
2019-02-08 13:01:15.861571-0500 Y! Finance[2610:591201] Downloaded image: 114522 B in 103745958 nanoseconds
2019-02-08 13:01:16.001535-0500 Y! Finance[2610:591191] Downloaded image: 110715 B in 139254166 nanoseconds
2019-02-08 13:01:16.134419-0500 Y! Finance[2610:591201] Downloaded image: 133672 B in 131134291 nanoseconds
2019-02-08 13:01:16.283966-0500 Y! Finance[2610:591283] Downloaded image: 221495 B in 148333875 nanoseconds
2019-02-08 13:01:16.448665-0500 Y! Finance[2610:591283] Downloaded image: 224138 B in 163167958 nanoseconds
2019-02-08 13:01:16.602229-0500 Y! Finance[2610:591201] Downloaded image: 181072 B in 151606958 nanoseconds
2019-02-08 13:01:16.725807-0500 Y! Finance[2610:591191] Downloaded image: 80539 B in 121599541 nanoseconds
2019-02-08 13:01:16.726130-0500 Y! Finance[2610:591191] Large images: 1583098995 nanoseconds
(1583098995 - 871286872) / 1e6 = 700ms
2019-02-08 13:05:42.475065-0500 Y! Finance[2618:592722] Downloaded image: 2608 B in 246357416 nanoseconds
2019-02-08 13:05:42.712309-0500 Y! Finance[2618:592714] Downloaded image: 2635 B in 236333166 nanoseconds
2019-02-08 13:05:42.811323-0500 Y! Finance[2618:592722] Downloaded image: 2488 B in 97600000 nanoseconds
2019-02-08 13:05:42.891436-0500 Y! Finance[2618:592720] Downloaded image: 1832 B in 78027125 nanoseconds
2019-02-08 13:05:42.966126-0500 Y! Finance[2618:592703] Downloaded image: 2535 B in 74035750 nanoseconds
2019-02-08 13:05:43.031131-0500 Y! Finance[2618:592773] Downloaded image: 2466 B in 64657250 nanoseconds
2019-02-08 13:05:43.105607-0500 Y! Finance[2618:592703] Downloaded image: 3014 B in 73647333 nanoseconds
2019-02-08 13:05:43.167938-0500 Y! Finance[2618:592722] Downloaded image: 1596 B in 61294875 nanoseconds
2019-02-08 13:05:43.233765-0500 Y! Finance[2618:592716] Downloaded image: 2371 B in 65415541 nanoseconds
2019-02-08 13:05:43.375313-0500 Y! Finance[2618:592716] Downloaded image: 1764 B in 141198625 nanoseconds
2019-02-08 13:05:43.480437-0500 Y! Finance[2618:592703] Downloaded image: 2459 B in 104686291 nanoseconds
2019-02-08 13:05:43.480570-0500 Y! Finance[2618:592703] Small images: 1243253372 nanoseconds
2019-02-08 13:05:43.622816-0500 Y! Finance[2618:592717] Downloaded image: 188022 B in 141416791 nanoseconds
2019-02-08 13:05:43.737771-0500 Y! Finance[2618:592773] Downloaded image: 159475 B in 113441791 nanoseconds
2019-02-08 13:05:43.847840-0500 Y! Finance[2618:592720] Downloaded image: 127945 B in 108202750 nanoseconds
2019-02-08 13:05:43.971360-0500 Y! Finance[2618:592774] Downloaded image: 81860 B in 121972125 nanoseconds
2019-02-08 13:05:44.113483-0500 Y! Finance[2618:592774] Downloaded image: 114522 B in 141465041 nanoseconds
2019-02-08 13:05:44.224854-0500 Y! Finance[2618:592716] Downloaded image: 110715 B in 110683250 nanoseconds
2019-02-08 13:05:44.411184-0500 Y! Finance[2618:592716] Downloaded image: 133672 B in 185705750 nanoseconds
2019-02-08 13:05:44.570402-0500 Y! Finance[2618:592720] Downloaded image: 221495 B in 158628291 nanoseconds
2019-02-08 13:05:44.768636-0500 Y! Finance[2618:592720] Downloaded image: 224138 B in 196850166 nanoseconds
2019-02-08 13:05:44.910529-0500 Y! Finance[2618:592720] Downloaded image: 181072 B in 139696791 nanoseconds
2019-02-08 13:05:45.037563-0500 Y! Finance[2618:592720] Downloaded image: 80539 B in 125538166 nanoseconds
2019-02-08 13:05:45.037772-0500 Y! Finance[2618:592720] Large images: 1543600912 nanoseconds
(1543600912-1243253372)/1e6 = 300ms
2019-02-08 13:06:30.356192-0500 Y! Finance[2618:592939] Downloaded image: 2608 B in 3090458 nanoseconds
2019-02-08 13:06:30.361711-0500 Y! Finance[2618:592924] Downloaded image: 2635 B in 3723666 nanoseconds
2019-02-08 13:06:30.370085-0500 Y! Finance[2618:592962] Downloaded image: 2488 B in 6689291 nanoseconds
2019-02-08 13:06:30.476456-0500 Y! Finance[2618:592967] Downloaded image: 1832 B in 105019166 nanoseconds
2019-02-08 13:06:30.477910-0500 Y! Finance[2618:592939] Downloaded image: 2535 B in 1024791 nanoseconds
2019-02-08 13:06:30.586271-0500 Y! Finance[2618:592939] Downloaded image: 2466 B in 108050250 nanoseconds
2019-02-08 13:06:30.643448-0500 Y! Finance[2618:592924] Downloaded image: 3014 B in 55932333 nanoseconds
2019-02-08 13:06:30.714522-0500 Y! Finance[2618:592967] Downloaded image: 1596 B in 70646166 nanoseconds
2019-02-08 13:06:30.753147-0500 Y! Finance[2618:592939] Downloaded image: 2371 B in 38063208 nanoseconds
2019-02-08 13:06:30.755117-0500 Y! Finance[2618:592939] Downloaded image: 1764 B in 1203666 nanoseconds
2019-02-08 13:06:30.757513-0500 Y! Finance[2618:592967] Downloaded image: 2459 B in 1823916 nanoseconds
2019-02-08 13:06:30.757572-0500 Y! Finance[2618:592967] Small images: 395266911 nanoseconds
2019-02-08 13:06:31.087399-0500 Y! Finance[2618:592962] Downloaded image: 188022 B in 329157750 nanoseconds
2019-02-08 13:06:31.241206-0500 Y! Finance[2618:592967] Downloaded image: 159475 B in 153201458 nanoseconds
2019-02-08 13:06:31.421663-0500 Y! Finance[2618:592967] Downloaded image: 127945 B in 179188083 nanoseconds
2019-02-08 13:06:31.546513-0500 Y! Finance[2618:592715] Downloaded image: 81860 B in 124088708 nanoseconds
2019-02-08 13:06:31.703267-0500 Y! Finance[2618:592967] Downloaded image: 114522 B in 155747833 nanoseconds
2019-02-08 13:06:31.880483-0500 Y! Finance[2618:592715] Downloaded image: 110715 B in 176039625 nanoseconds
2019-02-08 13:06:32.018577-0500 Y! Finance[2618:592939] Downloaded image: 133672 B in 137190166 nanoseconds
2019-02-08 13:06:32.024741-0500 Y! Finance[2618:592939] Downloaded image: 221495 B in 5687375 nanoseconds
2019-02-08 13:06:32.030696-0500 Y! Finance[2618:592939] Downloaded image: 224138 B in 5595500 nanoseconds
2019-02-08 13:06:32.036594-0500 Y! Finance[2618:592924] Downloaded image: 181072 B in 5239708 nanoseconds
2019-02-08 13:06:32.042656-0500 Y! Finance[2618:592715] Downloaded image: 80539 B in 5475708 nanoseconds
2019-02-08 13:06:32.042716-0500 Y! Finance[2618:592715] Large images: 1276611914 nanoseconds
(1276611914-395266911)/1e6 = 881ms
2019-02-08 13:08:39.490458-0500 Y! Finance[2618:593611] Downloaded image: 2608 B in 2297791 nanoseconds
2019-02-08 13:08:39.495485-0500 Y! Finance[2618:593611] Downloaded image: 2635 B in 3272083 nanoseconds
2019-02-08 13:08:39.499417-0500 Y! Finance[2618:593611] Downloaded image: 2488 B in 2762916 nanoseconds
2019-02-08 13:08:39.586223-0500 Y! Finance[2618:593679] Downloaded image: 1832 B in 85391916 nanoseconds
2019-02-08 13:08:39.595463-0500 Y! Finance[2618:593298] Downloaded image: 2535 B in 7293166 nanoseconds
2019-02-08 13:08:39.646303-0500 Y! Finance[2618:593611] Downloaded image: 2466 B in 49335625 nanoseconds
2019-02-08 13:08:39.683327-0500 Y! Finance[2618:593563] Downloaded image: 3014 B in 36582750 nanoseconds
2019-02-08 13:08:39.749052-0500 Y! Finance[2618:593598] Downloaded image: 1596 B in 64344291 nanoseconds
2019-02-08 13:08:39.801926-0500 Y! Finance[2618:593563] Downloaded image: 2371 B in 51012750 nanoseconds
2019-02-08 13:08:39.803770-0500 Y! Finance[2618:593456] Downloaded image: 1764 B in 1242458 nanoseconds
2019-02-08 13:08:39.805088-0500 Y! Finance[2618:593598] Downloaded image: 2459 B in 957875 nanoseconds
2019-02-08 13:08:39.805132-0500 Y! Finance[2618:593598] Small images: 304493621 nanoseconds
2019-02-08 13:08:39.992723-0500 Y! Finance[2618:593598] Downloaded image: 188022 B in 187275250 nanoseconds
2019-02-08 13:08:40.188615-0500 Y! Finance[2618:593456] Downloaded image: 159475 B in 195242750 nanoseconds
2019-02-08 13:08:40.333820-0500 Y! Finance[2618:593679] Downloaded image: 127945 B in 143734208 nanoseconds
2019-02-08 13:08:40.443736-0500 Y! Finance[2618:593599] Downloaded image: 81860 B in 108469416 nanoseconds
2019-02-08 13:08:40.580605-0500 Y! Finance[2618:593599] Downloaded image: 114522 B in 135011750 nanoseconds
2019-02-08 13:08:40.711250-0500 Y! Finance[2618:593679] Downloaded image: 110715 B in 129201500 nanoseconds
2019-02-08 13:08:40.887940-0500 Y! Finance[2618:593679] Downloaded image: 133672 B in 175595333 nanoseconds
2019-02-08 13:08:41.059703-0500 Y! Finance[2618:593567] Downloaded image: 221495 B in 170681041 nanoseconds
2019-02-08 13:08:41.253892-0500 Y! Finance[2618:593599] Downloaded image: 224138 B in 192629583 nanoseconds
2019-02-08 13:08:41.402162-0500 Y! Finance[2618:593599] Downloaded image: 181072 B in 146631416 nanoseconds
2019-02-08 13:08:41.536415-0500 Y! Finance[2618:593563] Downloaded image: 80539 B in 132497625 nanoseconds
2019-02-08 13:08:41.536681-0500 Y! Finance[2618:593563] Large images: 1716969872 nanoseconds
(1716969872-304493621)/1e6 = 1410ms
2019-02-08 13:09:27.110859-0500 Y! Finance[2618:593800] Downloaded image: 2608 B in 2368500 nanoseconds
2019-02-08 13:09:27.116819-0500 Y! Finance[2618:593810] Downloaded image: 2635 B in 4035666 nanoseconds
2019-02-08 13:09:27.120301-0500 Y! Finance[2618:593808] Downloaded image: 2488 B in 2575833 nanoseconds
2019-02-08 13:09:27.158132-0500 Y! Finance[2618:593757] Downloaded image: 1832 B in 36627125 nanoseconds
2019-02-08 13:09:27.160334-0500 Y! Finance[2618:593808] Downloaded image: 2535 B in 1847333 nanoseconds
2019-02-08 13:09:27.210162-0500 Y! Finance[2618:593800] Downloaded image: 2466 B in 49255125 nanoseconds
2019-02-08 13:09:27.253996-0500 Y! Finance[2618:593809] Downloaded image: 3014 B in 42833916 nanoseconds
2019-02-08 13:09:27.330840-0500 Y! Finance[2618:593800] Downloaded image: 1596 B in 75720916 nanoseconds
2019-02-08 13:09:27.404742-0500 Y! Finance[2618:593808] Downloaded image: 2371 B in 73348000 nanoseconds
2019-02-08 13:09:27.410989-0500 Y! Finance[2618:593759] Downloaded image: 1764 B in 5350250 nanoseconds
2019-02-08 13:09:27.413416-0500 Y! Finance[2618:593809] Downloaded image: 2459 B in 1691083 nanoseconds
2019-02-08 13:09:27.413483-0500 Y! Finance[2618:593809] Small images: 295653747 nanoseconds
2019-02-08 13:09:27.419675-0500 Y! Finance[2618:593800] Downloaded image: 188022 B in 5636166 nanoseconds
2019-02-08 13:09:27.424674-0500 Y! Finance[2618:593812] Downloaded image: 159475 B in 4387208 nanoseconds
2019-02-08 13:09:27.429942-0500 Y! Finance[2618:593759] Downloaded image: 127945 B in 4691250 nanoseconds
2019-02-08 13:09:27.479007-0500 Y! Finance[2618:593800] Downloaded image: 81860 B in 48550166 nanoseconds
2019-02-08 13:09:27.482588-0500 Y! Finance[2618:593800] Downloaded image: 114522 B in 3136875 nanoseconds
2019-02-08 13:09:27.485773-0500 Y! Finance[2618:593809] Downloaded image: 110715 B in 2904541 nanoseconds
2019-02-08 13:09:27.489005-0500 Y! Finance[2618:593809] Downloaded image: 133672 B in 2971250 nanoseconds
2019-02-08 13:09:27.492507-0500 Y! Finance[2618:593759] Downloaded image: 221495 B in 3205083 nanoseconds
2019-02-08 13:09:27.498394-0500 Y! Finance[2618:593809] Downloaded image: 224138 B in 5594708 nanoseconds
2019-02-08 13:09:27.506461-0500 Y! Finance[2618:593808] Downloaded image: 181072 B in 7186458 nanoseconds
2019-02-08 13:09:27.511982-0500 Y! Finance[2618:593813] Downloaded image: 80539 B in 4626500 nanoseconds
2019-02-08 13:09:27.512067-0500 Y! Finance[2618:593813] Large images: 92890205 nanoseconds
(92890205 - 295653747) = -202ms
(large_nanos - small_nanos) / 1e6 = delta_millis
(1583098995 - 871286872) / 1e6 = +700ms
(1543600912 - 1243253372)/1e6 = +300ms
(1276611914 - 395266911)/1e6 = +881ms
(1716969872 - 304493621)/1e6 = +1410ms
(92890205 - 295653747) = -202ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment