本地AI模型实现LRU 及其比较

gemma2(9.2b)

js 复制代码
class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map();
  }

  get(key) {
    if (this.cache.has(key)) {
      const value = this.cache.get(key);
      this.moveToFront(key); // 将访问过的 key 移到最前面
      return value;
    }
    return -1; // 键不存在
  }

  put(key, value) {
    if (this.cache.has(key)) {
      this.cache.set(key, value);
      this.moveToFront(key); // 更新值后,将 key 移到最前面
    } else {
      if (this.cache.size >= this.capacity) {
        // 缓存已满,移除最不经常使用的项
        const oldestKey = this.getOldestKey();
        this.cache.delete(oldestKey);
      }
      this.cache.set(key, value);
    }
  }

  moveToFront(key) {
    // 将 key 移到最前面
    const value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value);
  }

  getOldestKey() {
    // 返回最不经常使用的 key
    return Array.from(this.cache.keys())[0];
  }
}

// 示例用法
const cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
console.log(cache.get(1)); // 返回 1
cache.put(3, 3); // 移除 key 2
console.log(cache.get(2)); // 返回 -1
cache.put(4, 4); // 移除 key 1
console.log(cache.get(1)); // 返回 -1
console.log(cache.get(3)); // 返回 3
console.log(cache.get(4)); // 返回 4
kotlin 复制代码
class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map();
  }

  get(key) {
    if (this.cache.has(key)) {
      const value = this.cache.get(key);
      this.moveToFront(key); // 将访问过的 key 移到最前面
      return value;
    }
    return -1; // 键不存在
  }

  put(key, value) {
    if (this.cache.has(key)) {
      this.cache.set(key, value);
      this.moveToFront(key); // 更新值后,将 key 移到最前面
    } else {
      if (this.cache.size >= this.capacity) {
        // 缓存已满,移除最不经常使用的项
        const oldestKey = this.getOldestKey();
        this.cache.delete(oldestKey);
      }
      this.cache.set(key, value);
    }
  }

  moveToFront(key) {
    // 将 key 移到最前面
    const value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value);
  }

  getOldestKey() {
    // 返回最不经常使用的 key
    return Array.from(this.cache.keys())[0];
  }
}

// 示例用法
const cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
console.log(cache.get(1)); // 返回 1
cache.put(3, 3); // 移除 key 2
console.log(cache.get(2)); // 返回 -1
cache.put(4, 4); // 移除 key 1
console.log(cache.get(1)); // 返回 -1
console.log(cache.get(3)); // 返回 3
console.log(cache.get(4)); // 返回 4

解释:

  • LRUCache 类实现了一个容量为 capacity 的LRU 缓存。

  • cache 是一个 Map,用于存储键值对。

  • get(key) 方法:

    • 检查 key 是否存在于 cache 中。
    • 如果存在,则返回对应的值,并使用 moveToFront(key) 将 key 移到最前面。
    • 如果不存在,则返回 -1。
  • put(key, value) 方法:

    • 如果 key 已经存在,则更新对应的值并使用 moveToFront(key) 将 key 移到最前面。

    • 如果 key 不存在:

      • 如果 cache 已满,则使用 getOldestKey() 获取最不经常使用的 key 并从 cache 中删除。
      • 将新的 key 和 value 添加到 cache 中。
  • moveToFront(key) 方法:

    • 从 cache 中删除 key,然后将其重新添加到最前面。
  • getOldestKey() 方法:

    • 返回 cache 中的第一个键(即最不经常使用的键)。

phi3(3.8b)

js 复制代码
function LRUCache(capacity) {
  this.cache = []; // 存储元素的列表
  this.cacheMap = new Map(); // 存储元素及其索引关系
  this.capacity = capacity;
}

LRUCache.prototype.get = function(key) {
  if (this.cacheMap.has(key)) {
    let index = this.cacheMap.get(key);
    // 将该元素前移
    this.moveToFront(index);
    return this.cache[index];
  }
  return -1; // 返回-1表示不存在
};

LRUCache.prototype.put = function(key, value) {
  if (this.cacheMap.has(key)) {
    // 如果键已经在缓存中,则前移它到首位
    let index = this.cacheMap.get(key);
    this.moveToFront(index);
    this.cache[index] = value;
  } else {
    // 添加新元素
    if (this.cache.length >= this.capacity) {
      let leastRecentlyUsedKey = this.popLeastUsed();
      delete this.cacheMap.get(leastRecentlyUsedKey);
    }
    this.addToFront(key, value);
  }
};

LRUCache.prototype.remove = function(key) {
  if (this.cacheMap.has(key)) {
    let index = this.cacheMap.get(key);
    this.cache[index] = undefined; // 弹出元素值为undefined
    this.cacheMap.delete(key); // 从索引-值对象中删除
    if (this.cache.length < Math.ceil(this.capacity / 2)) {
      // 如果缓存容量低于一半的大小,则合并尾部元素与首部元素
      let lastIndex = this.cache.pop();
      if (lastIndex !== undefined) {
        delete this.cacheMap.get(this.cache[0]); // 删除首部元素对应索引-值对象中的关系
        this.addToFront(this.cache[lastIndex], this.cacheMap.keys().next().value);
      }
    }
  }
};

LRUCache.prototype.popLeastUsed = function() {
  if (this.cache.length === 0) return undefined; // 缓存为空时返回undefined
  let lastIndex = this.cache.length - 1;
  let leastRecentlyUsedKey = this.cache[lastIndex];
  delete this.cacheMap.get(leastRecentlyUsedKey);
  return leastRecentlyUsedKey; // 返回释放元素的索引
};

LRUCache.prototype.addToFront = function(key, value) {
  if (this.cache.length >= this.capacity) throw new Error('Capacity exceeded');
  this.cache.unshift(value); // 前移元素到首位
  let index = this.cache.length - 1;
  this.cacheMap.set(key, index); // 更新索引-值对象中关系
};

LRUCache.prototype.moveToFront = function(index) {
  if (this.cache.length <= index) throw new Error('Index out of range');
  let key = this.cache[index];
  this.remove(key); // 先移除以后再前移
  this.addToFront(key, this.cache[index]);
};

这个LRUCache类的主要方法包括:

  • get(key): 返回值对应于指定键的元素。如果没有查询到,则返回-1。
  • put(key, value): 将键值对存入缓存中,并根据最近使用到的规则移动元素。如果超过了容量限制,需要移除LRU(Least Recently Used)元素。
  • remove(key): 移除指定键对应的元素。

这个实现是简单且可以理解的,但在大规模或高性能场景下,可能需要更进一步的优化和改进。

llama3(8.0b)

kotlin 复制代码
class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map();
    this.order = [];
  }

  get(key) {
    if (!this.cache.has(key)) return null;
    const value = this.cache.get(key);
    this.remove(key);
    this.add(key);
    return value;
  }

  set(key, value) {
    if (this.cache.size >= this.capacity) {
      const evictedKey = this.order.shift();
      this.cache.delete(evictiedKey);
    }
    this.cache.set(key, value);
    this.add(key);
  }

  remove(key) {
    if (!this.cache.has(key)) return;
    this.cache.delete(key);
    const index = this.order.indexOf(key);
    if (index !== -1) this.order.splice(index, 1);
  }

  add(key) {
    this.cache.set(key, null); // Update the cache entry
    this.order.push(key);
  }
}

总结

这几个模型生成的代码各有侧重,需根据背景选择使用。

相关推荐
FungLeo9 分钟前
成为全栈·Vue 管理后台篇·Biome、类型检查、构建和API 代理如何组成质量门禁
前端·javascript·vue.js·typescript·vite·前端工程化·biome
夕水26 分钟前
受不了满屏广告的诗词网站,以及很多打不开的诗词网站,我干脆自己租服务器用trae重构了一个(纯公益、无广告)
前端·javascript
Brookty1 小时前
HTML基础知识:表格布局、表单数据提交、元素标识与资源相对路径
前端·html
徐小夕1 小时前
开源!我们用知识图谱重构了电子档案,AI 驱动全文智能关联
前端·vue.js·github
不一样的少年_1 小时前
用了这 Skill,前端兄弟都觉得好
前端·后端·ai编程
恋猫de小郭2 小时前
Flutter Native Interop 稳定了,Platform Channel 要退场了
android·前端·flutter
奶油话梅糖2 小时前
容器启停 2.5 毫秒:11 天 1,700 Star 的 Kiri,用一个 900 KB 的 PID 1 干掉了 runc 和 dockerd
前端·人工智能·rust
kyriewen16 小时前
编辑弹窗点了取消,列表里的数据怎么还是变了?
前端·javascript·人工智能
Karl_fang16 小时前
🥳 再也不用拿起手机抄验证码!我做了一个 Android → Mac 的「验证码接力」工具
前端·macos·ai编程