python 3.x:效率更高:列表与dict列表?

时间:2018-02-18 23:09:10

标签: python-3.x performance

成像您正在网格上进行BFS(例如两个单元格之间的最短距离)。两个数据结构可用于托管[HttpPost] public ActionResult RemoveItem(int? ID) { //pull existing cookie and get string value string cookie = ""; if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("CartCookie")) { cookie = this.ControllerContext.HttpContext.Request.Cookies["CartCookie"].Value; } if (cookie != "") { //convert string cookie into a dictionary Dictionary<string, string> keyValuePairs = cookie.Split('&') .Select(value => value.Split('=')) .ToDictionary(pair => pair[0], pair => pair[1]); //delete the existing cookie if it exists at all if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("CartCookie")) { HttpCookie deleteCookie = this.ControllerContext.HttpContext.Request.Cookies["CartCookie"]; deleteCookie.Expires = DateTime.Now.AddDays(-1); this.ControllerContext.HttpContext.Response.Cookies.Add(deleteCookie); System.Diagnostics.Debug.WriteLine("cookie deleted"); } // remove a specific key/value pair from the dictionary if (keyValuePairs.Remove(ID.Value.ToString())) { HttpCookie updatedCookie = Request.Cookies["CartCookie"]; if (updatedCookie == null) { // no cookie found, create it updatedCookie = new HttpCookie("CartCookie"); foreach (KeyValuePair<string, string> kvp in keyValuePairs) { updatedCookie.Values[kvp.Key] = kvp.Value; } } else { // update the cookie values foreach (KeyValuePair<string, string> kvp in keyValuePairs) { System.Diagnostics.Debug.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); updatedCookie.Values[kvp.Key] = kvp.Value; } } // update the expiration timestamp updatedCookie.Expires = DateTime.UtcNow.AddDays(30); // overwrite the cookie Response.Cookies.Add(updatedCookie); //print the value of the new cookie string newcookie = ""; if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("CartCookie")) { newcookie = this.ControllerContext.HttpContext.Request.Cookies["CartCookie"].Value; } System.Diagnostics.Debug.WriteLine(newcookie); } else { // dictionary doesn't contain above key } } return Json(true); } 信息:

1)列表清单,即cookie deleted //confirms that the cookie delete function runs Key = 1, Value = X Large //shows that only one item is in the NEW dictionary 1=X Large&2=Small //shows that the new cookie still has both items 。之后,我们可以通过visited访问特定单元格中的数据。

2)Dict,即data = [[False for _ in range(cols)] for _ in range(rows)]。之后,我们可以通过data[r][c]访问特定单元格中的数据。

我的问题是:在这种BFS场景中,哪种计算效率更高?

明智的编码似乎dict方法可以节省更多的字符/行。记忆方面,dict方法可以为未接触的单元格节省一些空间,但也可以为哈希表的额外空间浪费一些空间。

修改

@Peteris提到了numpy数组。列表列表的优势显而易见:numpy数组在连续的内存块上运行,这允许更快的寻址和更多的缓存命中。但是我不确定它们与哈希表的比较(即data = dict())。如果算法涉及的元素数量相对较少,那么哈希表可能会提供更多的缓存命中率,因为它可能会占用更小的内存。

另外,事实是我不能使用numpy数组。所以我真的需要将列表列表与dict进行比较。

1 个答案:

答案 0 :(得分:2)

2D数组

存储2D数据的有效答案是分配到连续存储区域中的2D数组/矩阵(不像列表列表)。这避免了否则需要多次内存查找,以及在dict所需的每次查找时计算哈希值。

在python中执行此操作的标准方法是使用numpy库,这是一个简单的示例

import numpy as np
data = np.zeros( (100, 200) ) # create a 100x200 array and initialize it with zeroes
data[10,20] = 1  # set element at coordinates 10,20 to 1