Files
swift-mirror/benchmark/single-source/BitCount.swift
Erik Eckstein 8abe785300 benchmarks: Fix the BitCount benchmark
*) Increase the iteration count, so that we cat a time value > 100 for better accuracy
*) Use getInt to prevent constant propgation and loop hoisting
2017-06-19 20:30:20 -07:00

43 lines
1.1 KiB
Swift

//===--- BitCount.swift ---------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This test checks performance of Swift bit count.
// and mask operator.
// rdar://problem/22151678
import Foundation
import TestsUtils
func countBitSet(_ num: Int) -> Int {
let bits = MemoryLayout<Int>.size * 8
var cnt: Int = 0
var mask: Int = 1
for _ in 0...bits {
if num & mask != 0 {
cnt += 1
}
mask <<= 1
}
return cnt
}
@inline(never)
public func run_BitCount(_ N: Int) {
var sum = 0
for _ in 1...1000*N {
// Check some results.
sum = sum &+ countBitSet(getInt(1))
&+ countBitSet(getInt(2))
&+ countBitSet(getInt(2457))
}
CheckResults(sum == 8 * 1000 * N)
}