ArrayLists 比数组慢 2 倍

2024-04-25

我正在测试一种分子动力学算法,该算法除其他外,还有一个 Particle 类,由9 双精度数组存储粒子分量(3D 环境中的速度、力和位置)。

我使用 5 个输入大小测试算法:

Size (MB) Time (s)
0.06      0.36     (fits in cache L2)
0.14      1.79     (fits in cache L2)
0.60      36.86    (fits in cache L3)
1.35      182.24   (fits in cache L3)
17.38     566.55   (it only fits in RAM)

比我改变Particles布局来自array to ArrayList。为了拥有连续的内存块,我创建了 arrayList ,其大小将占用:

ArrayList <Double> px = new ArrayList <Double>(Input_Size);

我在上述睾丸的相同条件下运行该版本,结果是:

Size (MB) Time (s)
0.06      0.608
0.14      2.78
0.60      57.15
1.35      299.24
17.38     1436,42

测试环境为:

AMD Opteron 处理器 6174,800 MHz,12 MB 三级缓存,24 核;

我的速度降低了大约 2 倍。这是正常的吗?不应该期望两个版本的时间几乎相同,因为ArrayList是像数组一样在内存中分配连续的吗?

EDIT:

Running with the option **-XX:+PrintCompilation**

  WITH ARRAY:

 1       java.util.jar.Manifest$FastInputStream::readLine (167 bytes)
  2       sun.nio.cs.UTF_8$Decoder::decodeArrayLoop (553 bytes)
  3       java.lang.String::hashCode (60 bytes)
  4       java.lang.String::charAt (33 bytes)
  5       sun.security.util.ManifestDigester::findSection (180 bytes)
  6       java.lang.Object::<init> (1 bytes)
  7       moldyn.random::update (104 bytes)
  8       moldyn.random::seed (80 bytes)
---   n   java.lang.StrictMath::log (static)
  9       java.lang.Math::log (5 bytes)
 10       moldyn.md::scalingVelocity (82 bytes)
 11       moldyn.Particles::distance (192 bytes)
  1%      moldyn.Particles::force @ 42 (211 bytes)
 12       moldyn.Particles::force (211 bytes)
 13       moldyn.Particles::domove (163 bytes)
 14       moldyn.Particles::domove_out (160 bytes)
  2%      moldyn.Particles::cicle_domove @ 5 (23 bytes)
 15       moldyn.Particles::update_force (49 bytes)
  3%      moldyn.Particles::cicle_forces @ 6 (27 bytes)
 16       moldyn.Particles::mkekin (141 bytes)
  4%      moldyn.Particles::cicle_mkekin @ 9 (33 bytes)
 17       moldyn.Particles::velavg (70 bytes)
  5%      moldyn.Particles::cicle_velavg @ 9 (37 bytes)
 18       moldyn.Particles::cicle_domove (23 bytes)
 19       moldyn.Particles::cicle_forces (27 bytes)
 20       moldyn.Particles::cicle_mkekin (33 bytes)
 21       moldyn.Particles::cicle_velavg (37 bytes)
36.763

WITH ArrayList <Double>....
----

  1       java.util.jar.Manifest$FastInputStream::readLine (167 bytes)
  2       sun.nio.cs.UTF_8$Decoder::decodeArrayLoop (553 bytes)
  3       java.lang.String::hashCode (60 bytes)
  4       java.lang.String::charAt (33 bytes)
  5       sun.security.util.ManifestDigester::findSection (180 bytes)
  6       java.lang.Object::<init> (1 bytes)
---   n   java.lang.System::arraycopy (static)
  7       java.lang.Number::<init> (5 bytes)
  8       java.util.ArrayList::ensureCapacity (58 bytes)
  9       java.lang.Double::valueOf (9 bytes)
 10       java.lang.Double::<init> (10 bytes)
 11       java.util.ArrayList::add (100 bytes)
 12       java.util.ArrayList::RangeCheck (48 bytes)
 13       java.util.ArrayList::set (21 bytes)
 14       moldyn.random::update (104 bytes)
 15       moldyn.random::seed (80 bytes)
---   n   java.lang.StrictMath::log (static)
 16       java.lang.Math::log (5 bytes)
 17       java.util.ArrayList::get (12 bytes)
 18       java.lang.Double::doubleValue (5 bytes)
 19       moldyn.md::scalingVelocity (120 bytes)
 20       moldyn.Particles::distance (240 bytes)
  1%      moldyn.Particles::force @ 42 (211 bytes)
 21       moldyn.Particles::force (211 bytes)
 22       moldyn.Particles::domove (337 bytes)
 23       moldyn.Particles::domove_out (292 bytes)
  2%      moldyn.Particles::cicle_domove @ 5 (23 bytes)
 24       moldyn.Particles::update_force (91 bytes)
  3%      moldyn.Particles::cicle_forces @ 6 (27 bytes)
 25       moldyn.Particles::mkekin (297 bytes)
  4%      moldyn.Particles::cicle_mkekin @ 9 (33 bytes)
 26       moldyn.Particles::velavg (118 bytes)
  5%      moldyn.Particles::cicle_velavg @ 9 (37 bytes)
 27       moldyn.Particles::cicle_domove (23 bytes)
 28       moldyn.Particles::cicle_forces (27 bytes)
 29       moldyn.Particles::cicle_mkekin (33 bytes)
 30       moldyn.Particles::cicle_velavg (37 bytes)
55.98

我有一些想法,但没有明确的答案:

  1. A java.lang.Double与 a 不一样double原始。可能是自动装箱开销和额外的机械伴随着Double对象有所作为。我会比较字节码,看看这是否属实。
  2. 听起来像是选择double [] or List<Double>对您内部的客户隐藏Particle班级。如果是这种情况,请使用数组,因为它是内部实现细节。
  3. 我会小心不要用基准测试来欺骗自己。
  4. 我想知道你的Particle类是否可变。这可能会有所作为。您的对象中的位置、速度和力是否不断变化并更新?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ArrayLists 比数组慢 2 倍 的相关文章

随机推荐

  • 这个 NLP 问题层次结构描述中的最大池化是什么类型

    我正在尝试实现这个描述以及我所做的 我生成了形状的 uni gram bi gram tri gram 15 512 使用填充 然后对于每个单词 我连接三个特征向量 3 512 然后我向他们申请 Globalmaxpooling1D 我不知
  • 检查每个页面请求的标头

    我需要检查每个页面请求的请求标头 我认为尝试这个的地方应该是在 撇号 express 模块中 我创建了自己的模块 auth 它扩展了 apostrope express module exports extend apostrophe ex
  • Vim - 删除多行中的直到(包括)字符

    我有这个代码 def foo c Char c match case a B 我的光标位于后面的空格上 我想删除所有内容 直到 包括 我怎样才能做到这一点 我可以在光标位于第一行任意位置时执行相同操作吗 块中的任何位置 并将光标放在 d e
  • 动态添加的 Facebook 发送按钮未呈现

    我想在页面加载后动态添加一个 Facebook 发送按钮 iframe 尚不支持 由于网站的构建方式 它将成为通过 AJAX 在用户上加载的 HTML 模板的一部分 行动 虽然我正在导入 FB JavaScript SDK 但当我通过 AJ
  • Protractor/Jasmine 条件测试用例

    与此问题相关 如何使用 Protractor 创建条件测试用例 https stackoverflow com questions 36701985 how can i create conditional test cases using
  • 我可以使用什么方法从 VBA 调用 Web 服务?

    从 Excel 调用 Web 服务的最简单方法是什么 我使用的是 2002 版 请不要使用第三方工具或库 这必须很容易使用一些 VBA 我可以将其粘贴到那里 使用它并随时触摸 我不认为有任何特别简单的方法可以直接从 VBA 与 SOAP 对
  • 如何使用 Web3.js 或 Ethers.js 强制传输 NFT

    我正在尝试构建一个应用程序 其中一部分涉及将 NFT 从我的帐户转移到用户的帐户 以前 我使用 Moralis 来完成此任务 但这段代码抛出了一个错误 Error Contract with a Signer cannot override
  • 如何在 Javascript 中从 Firefox 剪贴板获取内容

    我非常沮丧地在我的 React 应用程序上从剪贴板进行粘贴 I used navigator clipboard readText 在 Chrome 浏览器上完美运行 但它在我最新的 Firefox 浏览器上不起作用 我尝试搜索SO 但与之
  • Spring Data Redis - Lettuce连接池设置

    尝试在 spring data redis 环境中设置 Lettuce 连接池 下面是代码 Bean LettuceConnectionFactory redisConnectionFactory GenericObjectPoolConf
  • Gulp - 术语“gulp”未被识别为 cmdlet 的名称

    当我开始一个新项目并创建一个新的 gulpfile 时 开始出现这个随机错误 每当我跑步时我都会得到它 这不仅发生在这个项目上 而且已经开始发生在所有其他项目上 我读到环境变量可能存在问题 所以我更新了这些变量 我最近还运行了 ruby 安
  • 在 javascript 中格式化日期直至毫秒

    我们正在使用来自 Microsoft 的以下 js 库https ajax aspnetcdn com ajax 4 0 1 MicrosoftAjax js https ajax aspnetcdn com ajax 4 0 1 Micr
  • Angular 2 --aot 导致 AnimationEntryMetadata 失败

    我的代码可以正常工作ng build and ng build prod但是 当我将 oat 添加到命令中时 它会失败并出现以下错误 Uncaught Error Module build failed Error C Users drem
  • Google 气泡图自定义工具提示列不呈现

    我正在尝试将自定义工具提示添加到气泡图中 以替换默认的工具提示 我已按照文档网站的说明进行操作 here https developers google com chart interactive docs customizing tool
  • MultipartFile 文件名中的特殊字符转换为?在春季启动

    我想知道为什么 spring boot 将 MultiPartFile 文件名特殊字符转换为 例如 pdf 转换为 pdf 我需要配置 Spring 来禁用此行为吗 我已经检查了我的 jvm 配置中的 file encoding 它已经设置
  • DRY:如何在 Symfony2 项目的多个实体中使用此代码?特质?

    我有一段重复的代码 将在我的 Symfony2 项目中的多个实体中使用 因此应用某种 DRY 就可以了 当然如果可能的话 我正在考虑PHP 特性 http php net manual en language oop5 traits php
  • 如何在M1 arm64架构上安装PyQt5?

    我有一台 M1 mac 但我注意到 每当本机 python 运行任何自动化脚本 如 PyAutoGui 时 它都会逐渐变得越来越慢 几乎就像受到了限制一样 我用 Miniforge3 创建了一个能够利用 Apple 芯片的环境 使脚本运行得
  • 如何制作进度条

    如何在 html css javascript 中制作进度条 我真的不想使用Flash 可以在这里找到类似的内容 http dustincurtis com about html http dustincurtis com about ht
  • 缩放 ImageView 的图像,同时将中心点保持在同一位置

    我已将预缩放位图设置为 ImageView 的源 然后我读了矩阵ImageView并通过以下方式移动 ImageView 的位图matrix postTranslate shiftX shiftY 现在我想放大 缩小图像 同时保持中心Ima
  • Android Volley POST Json 到服务器

    我正在使用 Volley 在 Android 设备和网络服务器之间传输数据 我发现有关将数据列表发送到服务器的问题 例如 我的类将生成如下数据集 1 1 aID 5 2 aID 5 3 aID 5 4 aID 5 2 1 bID 3 2 b
  • ArrayLists 比数组慢 2 倍

    我正在测试一种分子动力学算法 该算法除其他外 还有一个 Particle 类 由9 双精度数组存储粒子分量 3D 环境中的速度 力和位置 我使用 5 个输入大小测试算法 Size MB Time s 0 06 0 36 fits in ca