第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用列表索引時(shí)如何加速“for”循環(huán)?(Python)

使用列表索引時(shí)如何加速“for”循環(huán)?(Python)

RISEBY 2023-12-29 16:47:43
我嘗試使用 Numpy 函數(shù)或向量而不是 for 循環(huán)來(lái)加速此代碼:sommes = []for j in range(vertices.shape[0]):    terme = new_vertices[j] - new_vertices[vertex_neighbors[j]]    somme_j = np.sum(terme)    sommes.append(somme_j)E_int = np.sum(sommes)(它是迭代算法的一部分,并且有很多“頂點(diǎn)”,所以我認(rèn)為 for 循環(huán)花費(fèi)的時(shí)間太長(zhǎng)。)例如,要計(jì)算 j = 0 時(shí)的“terme”,我有:In: new_vertices[0]Out: array([ 10.2533888 , -42.32279717,  68.27230793])In: vertex_neighbors[0]Out: [1280, 2, 1511, 511, 1727, 1887, 759, 509, 1023]In: new_verties[vertex_neighbors[0]]Out: array([[ 10.47121043, -42.00123956,  68.218715  ],            [ 10.2533888 , -43.26905874,  62.59473849],            [ 10.69773735, -41.26464083,  68.09594854],            [ 10.37030712, -42.16729601,  68.24639107],            [ 10.12158146, -42.46624547,  68.29621598],            [  9.81850836, -42.71158695,  68.33710623],            [  9.97615447, -42.59625943,  68.31788497],            [ 10.37030712, -43.11676015,  62.54960623],            [ 10.55512696, -41.82622703,  68.18954624]])In: new_vertices[0] - new_vertices[vertex_neighbors[0]]Out: array([[-0.21782162, -0.32155761,  0.05359293],             [ 0.        ,  0.94626157,  5.67756944],             [-0.44434855, -1.05815634,  0.17635939],             [-0.11691832, -0.15550116,  0.02591686],             [ 0.13180734,  0.1434483 , -0.02390805],             [ 0.43488044,  0.38878979, -0.0647983 ],             [ 0.27723434,  0.27346227, -0.04557704],             [-0.11691832,  0.79396298,  5.7227017 ],             [-0.30173816, -0.49657014,  0.08276169]])問(wèn)題是 new_vertices[vertex_neighbors[j]] 并不總是具有相同的大小。例如,當(dāng) j = 7 時(shí):In: new_vertices[7]Out: array([ 10.74106112, -63.88592276, -70.15593947])In: vertex_neighbors[7]Out: [1546, 655, 306, 1879, 920, 925]沒(méi)有for循環(huán)可以嗎?我的想法已經(jīng)用完了,所以任何幫助將不勝感激!
查看完整描述

1 回答

?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

是的,這是可能的。這個(gè)想法是用來(lái)np.repeat創(chuàng)建一個(gè)向量,其中的項(xiàng)目重復(fù)可變次數(shù)。這是代碼:


# The two following lines can be done only once if the indices are constant between iterations (precomputation)

counts = np.array([len(e) for e in vertex_neighbors])

flatten_indices = np.concatenate(vertex_neighbors)


E_int = np.sum(np.repeat(new_vertices, counts, axis=0) - new_vertices[flatten_indices])

這是一個(gè)基準(zhǔn):


import numpy as np

from time import *



n = 32768

vertices = np.random.rand(n, 3)

indices = []


count = np.random.randint(1, 10, size=n)


for i in range(n):

    indices.append(np.random.randint(0, n, size=count[i]))


def initial_version(vertices, vertex_neighbors):

    sommes = []

    for j in range(vertices.shape[0]):

        terme = vertices[j] - vertices[vertex_neighbors[j]]

        somme_j = np.sum(terme)

        sommes.append(somme_j)

    return np.sum(sommes)


def optimized_version(vertices, vertex_neighbors):

    # The two following lines can be precomputed

    counts = np.array([len(e) for e in indices])

    flatten_indices = np.concatenate(indices)


    return np.sum(np.repeat(vertices, counts, axis=0) - vertices[flatten_indices])


def more_optimized_version(vertices, vertex_neighbors, counts, flatten_indices):

    return np.sum(np.repeat(vertices, counts, axis=0) - vertices[flatten_indices])


timesteps = 20


a = time()

for t in range(timesteps):

    res = initial_version(vertices, indices)

b = time()

print("V1: time:", b - a)

print("V1: result", res)


a = time()

for t in range(timesteps):

    res = optimized_version(vertices, indices)

b = time()

print("V2: time:", b - a)

print("V2: result", res)


a = time()

counts = np.array([len(e) for e in indices])

flatten_indices = np.concatenate(indices)

for t in range(timesteps):

    res = more_optimized_version(vertices, indices, counts, flatten_indices)

b = time()

print("V3: time:", b - a)

print("V3: result", res)

這是我機(jī)器上的基準(zhǔn)測(cè)試結(jié)果:


V1: time: 3.656714916229248

V1: result -395.8416223057596

V2: time: 0.19800186157226562

V2: result -395.8416223057595

V3: time: 0.07983255386352539

V3: result -395.8416223057595

正如您所看到的,這一優(yōu)化版本比參考實(shí)現(xiàn)快 18 倍,而預(yù)先計(jì)算索引的版本比參考實(shí)現(xiàn)快 46 倍。


請(qǐng)注意,優(yōu)化版本應(yīng)該需要更多 RAM(特別是如果每個(gè)頂點(diǎn)的鄰居數(shù)量很大)。


查看完整回答
反對(duì) 回復(fù) 2023-12-29
  • 1 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)