xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

程序局部性原理 All In One

程序局部性原理 All In One

性能优化

局部性原理 / 引用局部性

在计算机科学中,引用局部性,也称为局部性原理,处理器在短时间内重复访问同一组内存位置的趋势。
引用局部性有两种基本类型——时间局部性和空间局部性。

  1. 时间局部性是指在相对较小的持续时间重用特定数据和(或)资源。
  2. 空间局部性(也称为数据局部性) 是指在相对较近存储位置内使用数据元素。
    顺序局部性是空间局部性的一种特殊情况,当数据元素线性排列和访问时发生,例如遍历一维数组中的元素。

局部性是计算机系统中发生的一种可预测行为。
表现出强大的引用局部性的系统是通过在处理器核心的流水线阶段使用缓存内存预取和高级分支预测器等技术进行性能优化的绝佳候选者。

https://en.wikipedia.org/wiki/Locality_of_reference

访问局部性

访问局部性(英语:Locality of reference)指的是在计算机科学领域中应用程序在访问内存的时候,倾向于访问内存中较为靠近的值。

访问局部性分为两种基本形式,一种是时间局部性,另一种是空间局部性。

时间局部性指的是,程序在运行时,最近刚刚被引用过的一个内存位置容易再次被引用,比如在调取一个函数的时候,前不久才调取过的本地参数容易再度被调取使用。

空间局部性指的是,最近引用过的内存位置以及其周边的内存位置容易再次被使用
空间局部性比较常见于循环中,比如在一个数列中,如果第3个元素在上一个循环中使用,则本次循环中极有可能会使用第4个元素。

第三种为循序局部性。

局部性是出现在计算机系统中的一种可预测行为
系统的这种强访问局部性,可以被用来在处理器内核的指令流水线中进行性能优化,如缓存,内存预读取以及分支预测

https://zh.wikipedia.org/wiki/访问局部性
https://zh.wikipedia.org/zh-hans/访问局部性

数组 vs 链表

数组: 内存地址连续,顺序读取
链表: 内存地址非连续,非顺序读取

如果数据以查找为主,很少涉及到增和删,选择数组;
如果数据涉及到频繁的插入删除,或元素所需分配内存空间过,则优先选择链表;

image

应用场景

链表反转,双指针

链表相等比较,快慢指针

demos

链表反转 / reverse-linked-list

# Definition for singly-linked list.

class ListNode:
  def __init__(self, val=0, next=None):
    self.val = val
    self.next = next

class Solution:
  def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
    node = None
    while head:
      # 缓存
      temp = head.next
      # 置空
      head.next = node
      # 赋值
      node = head
      # 恢复
      head = temp
    return node

from typing import Optional

class ListNode:
  def __init__(self, val=0, next=None):
    self.val = val
    self.next = next

class Solution:
  def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
    node = None
    while head:
      # 缓存
      temp = head.next
      # 置空
      head.next = node
      # 赋值
      node = head
      # 恢复
      head = temp
    return node


# def LinkedListGenerator(num: int) -> Optional[ListNode]:
#   head = ListNode()
#   temp = head
#   for i in range(num):
#     temp.next = ListNode(i + 1)
#     temp = temp.next
#   return head.next

def LinkedListGenerator(num: int, flag: bool = False) -> Optional[ListNode]:
  head = ListNode()
  temp = head
  for i in range(num):
    if flag == False:
      temp.next = ListNode(i + 1)
    else:
      temp.next = ListNode(num - i)
    temp = temp.next
  return head.next

head = LinkedListGenerator(5)
result = LinkedListGenerator(5, True)

solution = Solution()
node = solution.reverseList(head)

# print("node", node)

def compareListEqual(one, two):
  while one and two and one.val == two.val:
    one = one.next
    two = two.next
  # 同时为 None ✅
  if not one and not two:
    return True
  return False

test = compareListEqual(node, result)
if test:
  print("✅ test =", test)
else:
  print("❌ test =", test)

""" 

$ py3 ./206_reverse-linked-list.py
✅ test = True

https://leetcode.com/problems/reverse-linked-list/

"""

https://leetcode.com/problems/reverse-linked-list/description/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://www.cxyxiaowu.com/7913.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-08-12 22:07  xgqfrms  阅读(34)  评论(2编辑  收藏  举报