Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP';s array_slice vs Python';s分裂阵列 一些背景_Php_Python_Arrays - Fatal编程技术网

PHP';s array_slice vs Python';s分裂阵列 一些背景

PHP';s array_slice vs Python';s分裂阵列 一些背景,php,python,arrays,Php,Python,Arrays,我正在尝试常见的“最大利润”编程挑战。基本上是这样的: 给定一个由N个整数组成的零索引数组,其中包含 连续N天的股票价格,收益率 在此期间一笔交易的最大可能利润 我对自己提出的PHP算法非常满意,避免了天真的暴力尝试: public function maxProfit($prices) { $maxProfit = 0; $key = 0; $n = count($prices); while ($key < $n - 1) { $buy

我正在尝试常见的“最大利润”编程挑战。基本上是这样的:

给定一个由N个整数组成的零索引数组,其中包含 连续N天的股票价格,收益率 在此期间一笔交易的最大可能利润

我对自己提出的PHP算法非常满意,避免了天真的暴力尝试:

public function maxProfit($prices)
{
    $maxProfit = 0;
    $key = 0;
    $n = count($prices);

    while ($key < $n - 1) {
        $buyPrice = $prices[$key];
        $maxFuturePrice = max( array_slice($prices, $key+1) );          
        $profit = $maxFuturePrice - $buyPrice;
        
        if ($profit > $maxProfit) $maxProfit = $profit;
        $key++;
    }
    return $maxProfit;
}
结果:

$ php phpSlice.php
Time taken: 4473.9199ms
Time taken: 4474.633ms
Time taken: 4499.434ms
$ python pySlice.py 
Time taken: 213.202ms
Time taken: 212.198ms
Time taken: 215.7381ms
Time taken: 213.8121ms
pythona[s:e]

import time

n = 10000
a = range(0, n)

start = time.time()
for key, elem in enumerate(a):
    subArray = a[key : ]
end = time.time()

print "Time taken: {0}ms".format(round(1000 * (end - start), 4))
结果:

$ php phpSlice.php
Time taken: 4473.9199ms
Time taken: 4474.633ms
Time taken: 4499.434ms
$ python pySlice.py 
Time taken: 213.202ms
Time taken: 212.198ms
Time taken: 215.7381ms
Time taken: 213.8121ms
问题:
  • 为什么PHP的
    array\u slice()
    的效率比Python低20倍左右
  • PHP中是否有一种相当有效的方法可以实现上述目标,从而使我的
    maxprint
    算法在O(N)时间内运行编辑我意识到我上面的实现实际上不是O(N),但我的问题仍然是关于切片阵列的效率
  • 我真的不知道,但PHP的数组是混乱的混合怪物,也许这就是原因。Python的列表实际上只是列表,而不是同时使用字典,因此它们可能更简单/更快
  • 是的,做一个实际的O(n)解。您的解决方案之所以慢,不仅仅是因为PHP的切片显然很慢,还因为您显然有一个O(n^2)算法。只需浏览阵列一次,跟踪到目前为止找到的最低价格,并使用当前价格进行检查。不是像
    max
    那样在每次循环迭代中超过一半的数组

  • 啊,对。正确的动态规划方法显然是最好的,并且不需要每次对数组进行切片。根据这一点:如果指定一个长度,它会更快。(Python隐式地为您实现[key:-1])Big-O表示法与实现细节无关。它表示相对于输入大小计算输出所需的时间。它没有说明具体的实现运行了多少毫秒。无论您使用的语言有多快,您的算法仍然是O(n²)。我完全同意,但是我最初的问题出现了,因为在我看来,
    array_slice()
    函数本身在O(n)时间内运行(这导致我的实现是O(n²)),而Python的
    a[s:e]
    在O(1)时间内运行。但这很可能是错误的,我必须做进一步的测试来检查这一点/比我更有知识的人可以启发我。Python的
    a[s:e]
    也是O(N)的(N是切片的大小),因为它创建了该切片的副本。在n=100000(这是旧n的10倍)的情况下再次进行测试,您将看到它需要大约100倍的时间,而不是10倍。快速
    O(n)
    Python解决方案:
    from itertools import;来自操作员导入子系统;最大值(映射(子,库存,累计(库存,最小))