将数组的所有值设为负数或更改符号 [英] Make all values of an array negative or change sign

查看:173
本文介绍了将数组的所有值设为负数或更改符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SWIFT中,是否存在一种快速有效的方法来使数组的所有值都变为正负号(正值变为负,反之亦然)?

In SWIFT, is there a quick and efficient way to make all values of an array change sign (positive values become negative and vice-versa) ?

例如:

let x: [Double] =  [1.0, 2.0, -3.0, 4.0, -5.0]

应成为:

// x = [-1.0, -2.0, 3.0, -4.0, 5.0]


推荐答案

如果您需要大型阵列的快速解决方案,则可以使用
cblas_dscal() (标量乘法,双精度):

If you need a fast solution for large arrays then you can use cblas_dscal() ("scalar multiplication, double precision") from the Accelerate framework:

import Accelerate 

var x = [1.0, 2.0, -3.0, 4.0, -5.0]
cblas_dscal(Int32(x.count), -1.0, &x, 1)

print(x) // [-1.0, -2.0, 3.0, -4.0, 5.0]

备注:还有 cblas_sscal() $ c>浮动数组(单精度)。

Remark: There is also cblas_sscal() for Float arrays ("single precision").

性能比较:这是一个非常简单的性能比较$ b $各种方法中的b:

Performance comparison: Here is a quite simple performance comparison of the various methods:

let N = 1_000_000 // # of array elements
let R = 100 // # of repetitions

func test1() {
    var x = (0..<N).map { _ in drand48() }
    let start = Date()
    for _ in 0..<R {
        for i in x.indices { x[i] = -x[i] }
    }
    let time = Date().timeIntervalSince(start)
    print("x[i] = -x[i]     ", time)
}

func test2() {
    var x = (0..<N).map { _ in drand48() }
    let start = Date()
    for _ in 0..<R {
        for i in x.indices { x[i].negate() }
    }
    let time = Date().timeIntervalSince(start)
    print("x[i].negate()    ", time)
}

func test3() {
    var x = (0..<N).map { _ in drand48() }
    let start = Date()
    for _ in 0..<R {
        x = x.map { -$0 }
    }
    let time = Date().timeIntervalSince(start)
    print("x.map { -$0 }    ", time)
}

func test4() {
    var x = (0..<N).map { _ in drand48() }
    let start = Date()
    for _ in 0..<R {
        cblas_dscal(Int32(x.count), -1.0, &x, 1)
    }
    let time = Date().timeIntervalSince(start)
    print("cblas_dscal      ", time)
}

test1()
test2()
test3()
test4()

结果(在3.5 GHz英特尔处理器上iMac,已在发布配置中编译并运行:)

Results (on a 3.5 GHz Intel iMac, compiled and run in Release configuration:)


x[i] = -x[i]      0.0492849946022034
x[i].negate()     0.0635690093040466
x.map { -$0 }     0.285757005214691
cblas_dscal       0.0506410002708435

事实证明, cblas_dscal()的速度与
大致相同,显式循环的 x [i] = -x [i] x [i] .negate(),b ut比$ map()方法要快

As it turns out, cblas_dscal() has about the same speed as an explicit loop with x[i] = -x[i] or x[i].negate(), but is significantly faster than the map() method.

当然,在不同的硬件上,或者与
其他阵列大小相比,结果可能会有所不同。

Of course the results may be different on a different hardware, or with other array sizes.

与往常一样,从您最熟悉的方法开始,如果结果对性能至关重要,
会寻找更快的方法。

As usual, start with the method which you are most familiar with and look for faster methods if it turns out to be performance-critical.

这篇关于将数组的所有值设为负数或更改符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆