Skip to content

Commit

Permalink
Reduce frequency of progress indicator's output
Browse files Browse the repository at this point in the history
Only print the progress indicator every 65th loop iteration to reduce
CPU usage.

This mainly affect the terminal's CPU usage and not fdupes.
Mostly noticeable for large directories
  • Loading branch information
romuald committed Dec 30, 2024
1 parent 482509f commit b05d7aa
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions fdupes.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#include "xdgbase.h"
#endif

// Only print progress every Nth loop iteration
// Should be a (multiple of 4) + 1
#define PROGRESS_DIVIDER 65

char *program_name;

long long minsize = -1;
Expand Down Expand Up @@ -302,8 +306,12 @@ int grokdir(char *dir, file_t **filelistp, struct stat *logfile_status)

if (strcmp(dirinfo->d_name, ".") && strcmp(dirinfo->d_name, "..")) {
if (!ISFLAG(flags, F_HIDEPROGRESS)) {
fprintf(stderr, "\rBuilding file list %c ", indicator[progress]);
progress = (progress + 1) % 4;
// Only print progress every n-th file to reduce (terminal) CPU use
// (multiple of 4 + 1 so the indicator still alternate properly)
if ( progress % PROGRESS_DIVIDER == 0 ) {
fprintf(stderr, "\rBuilding file list %c ", indicator[progress % 4]);
}
progress++;
}

newfile = (file_t*) malloc(sizeof(file_t));
Expand Down Expand Up @@ -1855,8 +1863,11 @@ int main(int argc, char **argv) {
curfile = curfile->next;

if (!ISFLAG(flags, F_HIDEPROGRESS)) {
fprintf(stderr, "\rProgress [%d/%d] %d%% ", progress, filecount,
(int)((float) progress / (float) filecount * 100.0));
// Only print progress every 65th file to reduce (terminal) CPU used to display progress
if ( progress % PROGRESS_DIVIDER == 0 ) {
fprintf(stderr, "\rProgress [%d/%d] %d%% ", progress, filecount,
(int)((float) progress / (float) filecount * 100.0));
}
progress++;
}
}
Expand Down

0 comments on commit b05d7aa

Please sign in to comment.