Giter Site home page Giter Site logo

go-find-duplicates-benchmark's Introduction

go-find-duplicates-benchmark

This repository provides benchmarks to answer the question what's the most performant way to find duplicates present in a given slice of elements.

  • map[string]bool ?
  • map[string]interface{} ?
  • map[string]struct{} ?

Let's find it out! :D

So what's this about?

This code is a benchmark test for comparing the performance of three different versions of the findDuplicates function. The three versions are findDuplicatesTypeToBool, findDuplicatesTypeToInterface, and findDuplicatesTypeToStruct.

The benchmark test functions in main_test.go each call the respective findDuplicates function with a slice of strings that has a large number of elements and some identical duplicates. The benchmark test measures the performance of each findDuplicates function by running it a large number of times.

To run the benchmark test, you can use the go test command and pass the -bench flag:

โžœ  go-items-present-benchmark go test -bench .
goos: darwin
goarch: amd64
pkg: github.com/cbrgm/go-find-duplicates-benchmark
cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz

BenchmarkFindDuplicatesTypeToBool-8                    5         222490558 ns/op
BenchmarkFindDuplicatesTypeToInterface-8               4         276779110 ns/op
BenchmarkFindDuplicatesTypeToStruct-8                  5         217530765 ns/op

PASS
ok      github.com/cbrgm/go-find-duplicates-benchmark   8.067s

The winner is:

func findDuplicatesTypeToStruct(strings []string) []string {
	seenStrings := make(map[string]struct{})
	var duplicates []string
	for _, str := range strings {
		if _, ok := seenStrings[str]; ok {
			duplicates = append(duplicates, str)
		} else {
			seenStrings[str] = struct{}{}
		}
	}
	return duplicates
}

using make(map[string]struct{}).

go-find-duplicates-benchmark's People

Contributors

cbrgm avatar

Watchers

 avatar  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.