validating - Microbenchmarking

本文通过Java代码基准测试,对比了不同数组设置方法的运行效率。实验显示,在使用共享资源的情况下,并行处理并不总是更快。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Benchmarking means timing pieces of code or algorithms to see which runs faster.

// onjava/Timer.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package onjava;

import static java.util.concurrent.TimeUnit.*;

public class Timer {
  private long start = System.nanoTime();

  public long duration() {
    return NANOSECONDS.toMillis(System.nanoTime() - start);
  }

  public static long duration(Runnable test) {
    Timer timer = new Timer();
    test.run();
    return timer.duration();
  }
}
// validating/BadMicroBenchmark.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {ExcludeFromTravisCI}

import java.util.*;
import onjava.Timer;

public class BadMicroBenchmark {
  static final int SIZE = 250_000_000; // 250 million

  public static void main(String[] args) {
    try { // For machines with insufficient memory
      long[] la = new long[SIZE];
      System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> n))); // Arrays.setAll since 1.8
      System.out.println(
          "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> n))); // Arrays.parallelSetAll since 1.8
    } catch (OutOfMemoryError e) {
      System.out.println("Insufficient memory");
      System.exit(0);
    }
  }
}
/* My Output:
setAll: 217
parallelSetAll: 204
*/

 when depends on a common resource, the parallel version can end up being much slower, as the separate contend for that resource

// validating/BadMicroBenchmark2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Relying on a common resource

import java.util.*;
import onjava.Timer;

public class BadMicroBenchmark2 {
  // SIZE reduced to make it run faster:
  static final int SIZE = 5_000_000;

  public static void main(String[] args) {
    long[] la = new long[SIZE];
    Random r = new Random();
    System.out.println(
        "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> r.nextLong())));
    System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> r.nextLong())));
    SplittableRandom sr = new SplittableRandom(); // since 1.8, is designed for parallel algorithms
// This algorithm was inspired by the "DotMix" algorithm
    System.out.println(
        "parallelSetAll: " + Timer.duration(() -> Arrays.parallelSetAll(la, n -> sr.nextLong())));
    System.out.println("setAll: " + Timer.duration(() -> Arrays.setAll(la, n -> sr.nextLong())));
  }
}
/* My Output:
parallelSetAll: 946
setAll: 106
parallelSetAll: 73
setAll: 16
*/

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/onjava/Timer.java

3. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/System.java

4. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/concurrent/TimeUnit.java

5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark.java

6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/BadMicroBenchmark2.java

7. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/SplittableRandom.java

### Jupyter Notebook 扩展配置问题排查 当遇到 Jupyter Notebook 的 nbextensions 验证问题时,通常是因为扩展未正确安装、路径错误或依赖项缺失等原因引起的。以下是详细的解决方法: #### 1. 安装必要的工具 确保已安装 `jupyter_contrib_nbextensions` 和其支持文件。如果尚未安装,可以运行以下命令来完成安装[^1]: ```bash pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user ``` #### 2. 启用 Nbextensions Configurator 为了更方便地管理扩展,启用 Nbextensions Configurator 是非常重要的一步。通过执行以下命令实现[^2]: ```bash jupyter nbextensions_configurator enable --user ``` #### 3. 检查扩展验证失败的原因 如果某些扩展无法正常加载或者显示验证问题,可能有以下几个原因: - **JavaScript 文件损坏**:尝试重新下载并覆盖对应的 JavaScript 文件。 - **不兼容版本**:确认当前使用的 Jupyter 版本与扩展版本是否匹配。 - **权限不足**:以管理员身份运行终端或将扩展安装到全局环境。 可以通过查看浏览器控制台日志获取更多关于验证失败的信息。按下 F12 或右键 -> “检查”,切换至 Console 标签页观察是否有报错消息[^3]。 #### 4. 更新和修复扩展 对于已经存在的扩展,定期更新有助于减少潜在冲突。使用 pip 工具升级包: ```bash pip install --upgrade jupyter_contrib_nbextensions ``` 另外,也可以手动删除有问题的扩展目录后再重装一次。 #### 5. 测试新实例中的表现 创建一个新的 notebook 来测试这些修改后的效果如何。这可以帮助判断问题是特定于某个文档还是整个系统的设置上存在缺陷[^4]。 ```python import os print(os.getcwd()) # 确认工作目录无误 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值