在Objective C中的for循环中,alloc-init是否昂贵?

时间:2018-05-03 08:49:12

标签: objective-c for-loop initialization

我只是关于一般编程的问题。

我可以选择其中一个选项:

<?php
require('config.php');
include('auth.php');
$query = "SELECT name, faculty_id FROM faculty ORDER BY name ASC";
$result = mysqli_query($con, $query);
?>

<!DOCTYPE html>
<html>
<head></head>
<body>
<a href="/admin/"><< Go back</a>
<br><br>
<form action="/admin/departments/" method="post">
    <select name="faculties">
        <?php
        while ($row = mysqli_fetch_array($result)) {
            echo "<option value='" . $row['faculty_id'] . "'>" . $row['name'] . "</option>";
        }
        ?>
    </select>
</form>

<?php
if (isset($_POST['save_department'])) {
    $sql = "INSERT INTO department (name) VALUES ('" . $_POST["department_name"] . "')";

    $result = mysqli_query($con, $sql);
}
$query = "SELECT name, faculty_id, department_ID FROM department ORDER BY name ASC";
$result = mysqli_query($con, $query);
?>
<br><br>
<form action="/admin/departments/" method="post">
    <select name="departments">
        <?php
        while ($row = mysqli_fetch_array($result)) {
            echo "<option value='" . $row['department_ID'] . "'>" . $row['name'] . "</option>";
        }
        ?>
    </select>
</form>
<br>
<form method="POST" action="">
    <input name="department_name" type="text" required="required"><br>
    <br>
    <button type="submit" name="save_department">Add Department</button>
</form>
</body>
</html>

或者我可以选择:

for(some_conditions)
{
 NSFileManager * fm = [[NSFileManager alloc] init];
 BOOL result = [fm moveItemAtPath:x toPath:y error:&err];
}

我想知道的是,两者在执行这两个选项所花费的时间和空间方面是否有任何计算差异? 提前感谢一堆:)

1 个答案:

答案 0 :(得分:2)

如果您不打算使用NSFileManagerDelegate,请考虑使用defaultManager

NSFileManager *fm = NSFileManager.defaultManager;

它不仅被初始化一次,而且可以想象它可能会保留一些内部内存缓存来加速某些操作。

如果对性能感兴趣,您应该在代码中添加(和打印)时序,并使用真实数据进行尝试。我的猜测是,与alloc/init相比,moveItemAtPath的时间可以忽略不计。

可以在Xcode分配工具或调试存储器图中检查空间分配。我的猜测是两者都是一样的,因为在第一个变量中fm在每个循环迭代结束时被销毁。