I want to evaluate dynamically generated mathematical expressions using NSExpression
. When I run the simple program below, memory consumption quickly sums up to 1 GB and more.
Am I obviously leaking memory or is there anything wrong with the way I am using NSExpression
?
#!/usr/bin/env swift
import Foundation
for _ in 1...100 {
let expressionString = "((x - y) * ((x * x) - (((x - y) * -1) - y))) + (x * (((x * (y - x)) - x) * -1))"
let expression = NSExpression(format: expressionString)
for x in 0 ..< 320 {
for y in 0 ..< 320 {
let result = expression.expressionValue(with: ["x" : x, "y": y], context: nil) as! Double
}
}
}
In the real program, of course, I create a new expressionString
for every iteration of the outer loop.