Kerollos Magdy Profile picture
Feb 10 8 tweets 2 min read
Today I made a simple script that traverses a directory to find similar files according to their MD5 hash value.

github.com/kerolloz/same-…

The reason why I was trying to solve this problem is that I have a directory with a huge number of PDF files (books).
I was almost sure that there are some duplicates among these files (maybe even with different names, that's why the hashing).

I started by implementing a simple and straightforward script in Node.js.
It recursively traverses the directory and its sub-directories while collecting the files in each one of them. Then, generating a hash value for each file. Keeping a record of files that have the same hash value. Finally, printing out the full paths of similar ones.
That script ran peacefully on the books directory which has approximately 400 files (3.6 GB). It took ~1m 40s.
But can we do better? I thought that by using promises and trying to concurrently hash the files we can drastically improve the performance. But I was completely wrong.
pastebin.com/ALZxXm8p
Running the promises version took forever, I had to kill the process, and it was eating up all my RAM. So, I decided to implement the solution in Go.

A simple implementation in Go took almost (-10s) the same time as the first Node.js solution.
Now trying to improve our Go solution by implementing concurrency using Go routines & Wait Groups.
pastebin.com/0cfHSiY4
This solution took roughly 9 minutes. But the cool thing is it didn't consume much memory. The memory consumption was nearly consistent of about 6 MB.
We conclude that the bottleneck is the I/O (the disk reading speed, HDD in my case). No matter how we try to increase the performance, we are faced by the limitations of the physical movement of the hard disk (required to read the file, thus generating the hash value).
Let me know if I have any mistakes. 😁

I had so much fun today and I learned a lot.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Kerollos Magdy

Kerollos Magdy Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(