简体   繁体   English

PHP数组或字符串的性能和内存使用情况

[英]PHP Array Or string performance and memory usage

Option 1 or Option 2 which is the most feasible way in terms of memory and performance usage to append multiple email ids in the following loop. 就内存和性能使用而言,选项1或选项2是在以下循环中附加多个电子邮件ID的最可行方法。

Option 1: String $errorString 选项1:字符串$errorString

while (!feof($file_handle)) {  
    $errorString .= $email."<br>";
}

Option 2: Array $errorArray[] 选项2:数组$errorArray[]

while (!feof($file_handle)) {  
    $errorArray[].= $email."<br>";
}

The array will use a little more memory, because it needs to store all the strings and all the array elements that refer to them. 该数组将使用更多的内存,因为它需要存储所有字符串以及引用它们的所有数组元素。

The string will take more time, because every time you concatenate it has to copy the both strings into the new value of $errorString . 该字符串将花费更多时间,因为每次连接时,都必须将两个字符串都复制到$errorString的新值中。

Unless you have thousands of emails it's unlikely to make any noticeable difference, so you should use whichever method makes the rest of the code easier. 除非您有成千上万的电子邮件,否则不可能有任何明显的不同,因此,无论哪种方法,都应使其余代码更容易使用。 Premature optimization is the root of all evil. 过早的优化是万恶之源。

Well then. 好吧。 In most cases, the string would undergo minimal processing in the background to have to a append a substring to it. 在大多数情况下,字符串将在后台进行最少的处理,因此必须向其附加一个子字符串。 However, there are a few noticeable things there: 但是,那里有一些值得注意的事情:

1. You will do some processing later with the string or array you create. 1.稍后将对您创建的字符串或数组进行一些处理。 If you are to display the email IDs to the user without individual formatting, then you'd probably display the string as it is without any further requirement. 如果要向用户显示电子邮件ID而无需单独设置格式,则可能会按原样显示字符串,而无需任何其他要求。 This also applies when you want to store the final result in a database or just anything that would just take the whole string. 当您要将最终结果存储在数据库中或仅存储整个字符串的任何内容时,这也适用。 This would require you to first implode the array if you used the array then use the string, thus making the string the better alternative. 如果使用数组,则需要先内implode数组,然后使用字符串,从而使字符串成为更好的选择。

However, if you need to present these as separate fields, then you'd be required to first explode the string to the different portions before you can use them. 但是,如果您需要出示这些作为单独的领域,然后你会被要求先explode字符串的不同部分,然后才能使用它们。 Again, you would incur more memory usage if you used a string. 同样,如果使用字符串,则会导致更多的内存使用。

The point is, it depends with what you are going to do with the variable. 关键是,这取决于您要对变量执行的操作。

2. How big will the variable be? 2.变量有多大? This is actually the most important issue. 这实际上是最重要的问题。 If you are going to have just a little content in the variable, say something below a thousand members, then your choice wouldn't really matter unless you would be doing something quite memory intensive with each of them. 如果您只需要在变量中包含少量内容,例如说一千个以下的成员,那么您的选择就没有关系了,除非您要对每个成员进行大量占用内存的工作。 If you wouldn't be doing several million iterations, then I guess the difference in performance of the two would be negligible, which means the choice you make doesn't really matter. 如果您不打算进行数百万次迭代,那么我想两者的性能差异可以忽略不计,这意味着您所做的选择并不重要。

PS: when you assign a varibale to array, you don't use array [] .= something as you did in the question. PS:当您为数组分配变量时,您不会像在问题中那样使用array [] .= something You do array[] = something since you are appending the something to array rather than to array[index] . 之所以要array[] = something因为您将something附加到array而不是array[index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM