跳到主要內容

發表文章

目前顯示的是 9月, 2017的文章

JMH 筆記

Nolan Issac 使用 JMH 进行微基准测试:不要猜,要测试! 這幾天讀了幾篇很有趣的文章, 是關於 lambda 跟 jvm 效能評估的文章, 分別是 Java8 Lambda表达式和流操作如何让你的代码变慢5倍 使用JMH进行微基准测试:不要猜,要测试! JMH JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM. JMH 是用來衡量 JVM 容器上運作的 (Java, Scala, Kotlin, Groovy, Clojure, etc.) 效能的工具, 官方建議透過 maven 來建立測試專案, 可以避免一些奇怪設定影響效能的問題, groupdId 就替換成自己的 package name 吧, artifactId 就替換成測試的 project name, 會按照這個 project name 在當下路徑建立一個資料夾, mvn archetype:generate \ -DinteractiveMode=false \ -DarchetypeGroupId=org.openjdk.jmh \ -DarchetypeArtifactId=jmh-java-benchmark-archetype \ -DgroupId=com.example \ -DartifactId=jmh-benchmark \ -Dversion=1.0 maven Builde 出來的 code import org.openjdk.jmh.annotations.Benchmark; public class MyBenchmark { @Benchmark public void testMethod() { // This is a demo/sample template for building your JMH ben...

[Leetcode] 21. Merge Two Sorted Lists

題目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 看完題目, 以為可以用 LinkedList 這類的東西, 想說 ez ... 結果下面給的 hint Sample 根本就不是那麼一回事啊, 抓頭抓抓抓... /** * Definition for singly-linked list. * */ public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } 先拿到測試資料 /** * Created by jerry on 2017/9/24. * * Merge two sorted linked lists and return it as a new list. * The new list should be made by splicing together the nodes of the first two lists. */ public class LeetCode21MergeTwoSortedListsTest { private LeetCode21MergeTwoSortedLists sol = new LeetCode21MergeTwoSortedLists(); @Test public void test1() { final ListNode l1 = null; final ListNode l2 = new ListNode(0); ListNode act = sol.mergeTwoLists(l1, l2); Assert.assertEquals(0, act.val); } @Test public void test2() { final ListNode l1 = new ListNod...

[Leetcode] 1. TwoSum

建立測試案例 public class LeetCode1TwoSumTest { final LeetCode1TwoSum twoSum = new LeetCode1TwoSum(); /** * Example: * Given nums = [2, 7, 11, 15], target = 9, * Because nums[0] + nums[1] = 2 + 7 = 9, * return [0, 1]. */ @Test public void test1() { final int[] nums = new int[]{2, 7, 11, 15}; final int target = 9; int[] act = twoSum.twoSum(nums, target); Assert.assertTrue(IntStream.of(act).anyMatch(x -> x == 0)); Assert.assertTrue(IntStream.of(act).anyMatch(x -> x == 1)); } @Test public void test2() { final int[] nums = new int[]{3, 3}; final int target = 6; int[] act = twoSum.twoSum(nums, target); Assert.assertTrue(IntStream.of(act).anyMatch(x -> x == 0)); Assert.assertTrue(IntStream.of(act).anyMatch(x -> x == 1)); } @Test public void test3() { final int[] nums = new int[]{3, 2, 4}; final int target = 6; ...