# Question about a swift-collections-benchmark API

**URL:** <https://forums.swift.org/t/question-about-a-swift-collections-benchmark-api/75025>\
**Category:** Collections\
**Created:** [September 30, 2024, 10:09am UTC](https://forums.swift.org/t/question-about-a-swift-collections-benchmark-api/75025 "2024-09-30T10:09:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrystDragon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crystdragon/32/10461_2.png) [@CrystDragon](https://forums.swift.org/u/CrystDragon)\
**Post date:** [September 30, 2024, 10:09am UTC](https://forums.swift.org/t/question-about-a-swift-collections-benchmark-api/75025/1 "2024-09-30T10:09:55Z")

</div>

Hi, I've been extensively use this swift-collections-benchmark package lately, and I just noticed this API `Benchmark.add`. This API provides a `Timer` parameter to my custom closure.

However, I have trouble understanding how to use this parameter. I thought I could use it to achieve partial measuring, like this

```swift
benchmark.add(title: "xxx", input: Int.self) { count in
    return { timer in
        someSetup()
        timer.measure {
            coreLogic()
        }
    }
}

```

However, the above code will trigger a runtime assertion.

 ![image](https://global.discourse-cdn.com/swift/original/3X/b/1/b188a98e2c816ff7bbc9982dd0c8c6ea3860ca3d.png)

Can someone tell me the difference between `Benchmark.add` and `benchmark.addSimple`, and furthermore, how to use `Benchmark.add` correctly.

---

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [October 4, 2024, 11:15pm UTC](https://forums.swift.org/t/question-about-a-swift-collections-benchmark-api/75025/2 "2024-10-04T23:15:44Z")

</div>

That is in fact the right pattern!

It's hard to tell what goes wrong without looking at the sources. But I'd first check for one of the following issues:

1. `timer.measure` is only expected to be called at most once. Double check there is no way for the outer closure to end up calling it multiple times. For example, this will not work correctly:

2. The tasks are assumed to always execute the same way when given the same input -- if the task executes `timer.measure`, then it must do so every time. For example, this will not work correctly:

The `add` method allows the task to refuse to run on some inputs (by returning nil), and it lets you measure only part of the task's execution. `addSimple` is for the simple case, when you always want to run the task, and you want to measure the entire duration.

---

<div class="post-metadata">

**Author:** ![CrystDragon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crystdragon/32/10461_2.png) [@CrystDragon](https://forums.swift.org/u/CrystDragon)\
**Post date:** [October 5, 2024, 3:49pm UTC](https://forums.swift.org/t/question-about-a-swift-collections-benchmark-api/75025/3 "2024-10-05T15:49:34Z")

</div>

Thank you, that's really helpful! I'll look into my code.
