Skip to content

Commit

Permalink
Sample code for printf timing
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankGalligan committed Feb 15, 2023
1 parent 057010d commit f50dd7e
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,63 @@ static void ipmaPush(struct ipmaArray * ipma, uint8_t assoc, avifBool essential)
++ipma->count;
}

// Note: Code copied from libwebp for illustrative purposes. Once direction is decided the code would be added to libavif properly.
#ifndef _MSC_VER
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
#define WEBP_INLINE inline
#else
#define WEBP_INLINE
#endif
#else
#define WEBP_INLINE __forceinline
#endif /* _MSC_VER */


#if defined _WIN32 && !defined __GNUC__
#include <windows.h>

typedef LARGE_INTEGER Stopwatch;

static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
QueryPerformanceCounter(watch);//
}

static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
const LARGE_INTEGER old_value = *watch;
LARGE_INTEGER freq;
if (!QueryPerformanceCounter(watch))
return 0.0;
if (!QueryPerformanceFrequency(&freq))
return 0.0;
if (freq.QuadPart == 0)
return 0.0;
return (watch->QuadPart - old_value.QuadPart) / (double)freq.QuadPart;
}


#else /* !_WIN32 */
#include <sys/time.h>

typedef struct timeval Stopwatch;

static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
gettimeofday(watch, NULL);
}

static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
struct timeval old_value;
double delta_sec, delta_usec;
memcpy(&old_value, watch, sizeof(old_value));
gettimeofday(watch, NULL);
delta_sec = (double)watch->tv_sec - old_value.tv_sec;
delta_usec = (double)watch->tv_usec - old_value.tv_usec;
return delta_sec + delta_usec / 1000000.0;
}

#endif /* _WIN32 */


// Used to store offsets in meta boxes which need to point at mdat offsets that
// aren't known yet. When an item's mdat payload is written, all registered fixups
// will have this now-known offset "fixed up".
Expand Down Expand Up @@ -1216,8 +1273,8 @@ static avifResult avifEncoderAddImageInternal(avifEncoder * encoder,
cellImage = paddedCellImage;
}
const int quantizer = item->alpha ? encoder->data->quantizerAlpha : encoder->data->quantizer;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
Stopwatch watch;
StopwatchReset(&watch);
avifResult encodeResult = item->codec->encodeImage(item->codec,
encoder,
cellImage,
Expand All @@ -1228,7 +1285,7 @@ static avifResult avifEncoderAddImageInternal(avifEncoder * encoder,
encoderChanges,
addImageFlags,
item->encodeOutput);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
double deltaSec = StopwatchReadAndReset(&watch);
if (paddedCellImage) {
avifImageDestroy(paddedCellImage);
}
Expand All @@ -1238,8 +1295,8 @@ static avifResult avifEncoderAddImageInternal(avifEncoder * encoder,
if (encodeResult != AVIF_RESULT_OK) {
return encodeResult;
}
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("encode-encodeImage-%g-milli\n", delta_us / 1000.0);
double megapixelsPerSecEncode = ((cellImage->width * cellImage->height) / 1000000.0) / deltaSec;
printf("encodeImage %g milliseconds MP/s: %g\n", deltaSec * 1000.0, megapixelsPerSecEncode);
}
}

Expand Down

0 comments on commit f50dd7e

Please sign in to comment.