Skip to content

Instantly share code, notes, and snippets.

@dmahapatro
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmahapatro/0a4b785c80f11ee5dab1 to your computer and use it in GitHub Desktop.
Save dmahapatro/0a4b785c80f11ee5dab1 to your computer and use it in GitHub Desktop.
Benchmark performance between find and in list to match a key-value from a map present in list.
@Grab(group='com.googlecode.gbench', module='gbench', version='11.07.05')
def r = benchmark {
'findTestWithSmallerListSize' {
def myList = [[first: 'John', last: 'test']]
myList.find { it.first == 'John' } ? true : false
}
'inListTestWithSmallerListSize' {
def myList = [[first: 'John', last: 'test']]
'John' in myList*.first
}
'findTestWithMatchPresentInEnd' {
def myList = []
10000.times { myList << [first: 'Johnx', last: 'test'] }
myList << [first: 'John', last: 'test']
myList.find { it.first == 'John' } ? true : false
}
'inListTestWithMatchPresentInEnd' {
def myList = []
10000.times { myList << [first: 'Johnx', last: 'test'] }
myList << [first: 'John', last: 'test']
'John' in myList*.first
}
'findTestWithMatchPresentInAverageCenter' {
def myList = []
5000.times { myList << [first: 'Johnx', last: 'test'] }
myList << [first: 'John', last: 'test']
5000.times { myList << [first: 'Johnx', last: 'test'] }
myList.find { it.first == 'John' } ? true : false
}
'inListTestWithMatchPresentInAverageCenter' {
def myList = []
5000.times { myList << [first: 'Johnx', last: 'test'] }
myList << [first: 'John', last: 'test']
5000.times { myList << [first: 'Johnx', last: 'test'] }
'John' in myList*.first
}
'findTestWithMatchPresentInFirst' {
def myList = []
myList << [first: 'John', last: 'test']
10000.times { myList << [first: 'Johnx', last: 'test'] }
myList.find { it.first == 'John' } ? true : false
}
'inListTestWithMatchPresentInFirst' {
def myList = []
myList << [first: 'John', last: 'test']
10000.times { myList << [first: 'Johnx', last: 'test'] }
'John' in myList*.first
}
}
r.prettyPrint()
//Result
user system cpu real
findTestWithSmallerListSize 571 0 571 598
inListTestWithSmallerListSize 411 0 411 411
findTestWithMatchPresentInEnd 2714872 43 2714915 2739768
inListTestWithMatchPresentInEnd 2266303 39 2266342 2431595
findTestWithMatchPresentInAverageCenter 2141836 39 2141875 2263714
inListTestWithMatchPresentInAverageCenter 2261387 37 2261424 2438485
findTestWithMatchPresentInFirst 1667364 27 1667391 1813254
inListTestWithMatchPresentInFirst 2288471 37 2288508 2425089
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment