小编Gor*_*vic的帖子

最小化递归函数中的变量副本

在处理递归函数时,我正在寻找有效的内存分配。据我所知,我在函数中使用的变量将保持分配在内存中,直到递归完成。有没有办法避免这种情况,因为我相信这会导致我的代码运行缓慢,每次调用函数时都会复制状态变量(如果我错了,请纠正我,因为我是 C++ 新手)。

#include <fstream>
#include <vector>
using namespace std;

int N = 30;
double MIN_COST = 1000000;
vector<int> MIN_CUT = {};


void minCut(vector<int> state, int index, int nodeValue) {
    double currentCost;

    if (index >= 0) {
        currentCost = getCurrentCost(state); // some magic evaluating state cost
        state.push_back(nodeValue);
        if (currentCost >= MIN_COST) {    // kill branch if incomplete solution is already worse than best achieved solution
            return;
        }
    }

    if (index == N - 1) {   // check if leaf node …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
154
查看次数

Python + Selenium有效地获取列表中元素的属性

使用Python,Selenium和PhantomJS获取多个元素的相同属性的最有效方法是什么?我的解决方案使用find_elements_by_css_selector哪个定位我需要的所有元素,这需要不到一秒钟,然后我循环遍历列表以获取我需要的属性.我的循环花费了大约2500个元素的一分钟,考虑到所有元素都是用find_elements_by_css_selector方法映射的,这对我来说似乎很多.是get_attribute方法真的那么贵还是我做错了什么?

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path=r'mypath\phantomjs.exe')
driver.set_window_size(1120, 550)
driver.get("https://www.something.com")

table = []
elements = driver.find_elements_by_css_selector("tr[id*='bet-']") # takes under 1 second

for element in elements:
   table.append(element.get_attribute('data-info')) # takes over 60 seconds (2000 elements)

driver.close
Run Code Online (Sandbox Code Playgroud)

python selenium phantomjs selenium-webdriver

2
推荐指数
1
解决办法
3477
查看次数

标签 统计

c++ ×1

phantomjs ×1

python ×1

selenium ×1

selenium-webdriver ×1