GVKun编程网logo

为什么 Java switch on contiguous ints 似乎在添加案例时运行得更快?(switch例子java)

16

在这篇文章中,我们将为您详细介绍为什么Javaswitchoncontiguousints似乎在添加案例时运行得更快?的内容,并且讨论关于switch例子java的相关问题。此外,我们还会涉及一些关于

在这篇文章中,我们将为您详细介绍为什么 Java switch on contiguous ints 似乎在添加案例时运行得更快?的内容,并且讨论关于switch例子java的相关问题。此外,我们还会涉及一些关于.contiguous() 在 PyTorch 中做了什么?、19.1.2 Switch Virtual Interface Configuration、525. Contiguous Array、994.Contiguous Array 邻近数组的知识,以帮助您更全面地了解这个主题。

本文目录一览:

为什么 Java switch on contiguous ints 似乎在添加案例时运行得更快?(switch例子java)

为什么 Java switch on contiguous ints 似乎在添加案例时运行得更快?(switch例子java)

我正在处理一些需要高度优化的 Java
代码,因为它将在我的主程序逻辑中的许多点调用的热函数中运行。此代码的一部分涉及将double变量乘以10任意非负int
exponents。获得相乘值的一种快速方法(编辑:但不是最快的,请参见下面的更新 2)是switchexponent

double multiplyByPowerOfTen(final double d,final int exponent) {
   switch (exponent) {
      case 0:
         return d;
      case 1:
         return d*10;
      case 2:
         return d*100;
      // ... same pattern
      case 9:
         return d*1000000000;
      case 10:
         return d*10000000000L;
      // ... same pattern with long literals
      case 18:
         return d*1000000000000000000L;
      default:
         throw new ParseException("Unhandled power of ten " + power,0);
   }
}

上面注释的省略号表示case int常量继续递增 1,因此上面的代码片段中确实有 19
cases。由于我不确定在case语句10thru中是否真的需要 10
的所有幂18,我运行了一些微基准测试,比较了使用此switch语句完成 1000 万次操作的时间switch与仅使用cases
0thru9的时间(exponent限制为 9
或更少避免打破精简switch)。我得到了一个相当令人惊讶的结果(至少对我来说!),越长switchcase语句实际上运行得越快。

在云雀中,我尝试添加更多case的 s ,它只是返回了虚拟值,并发现我可以让开关运行得更快,大约 22-27 个声明case的 s
(即使在代码运行时这些虚拟案例实际上从未被命中) )。(同样,通过将先前常数case增加 s 以连续方式添加 s 。)这些执行时间差异不是很显着:对于
和 之间的随机,虚拟填充语句在 1.49 秒内完成 1000 万次执行,而未填充语句为 1.54 秒版本,每次执行总共节省
5ns。所以,不是那种让人痴迷于填充的东西case``1``exponent``0``10``switch``switch从优化的角度来看,声明值得付出努力。但是我仍然觉得奇怪和违反直觉的是,当添加更多的
s 时,它的 执行switch速度并没有变慢(或者可能充其量保持恒定的 O(1) 时间) 。case

切换基准测试结果

这些是我通过对随机生成的exponent值进行各种限制而获得的结果。我没有将结果一直计算到1极限exponent,但曲线的总体形状保持不变,在
12-17 的情况下有一个山脊,在 18-28 之间有一个谷。所有测试都在 JUnitBenchmarks
中运行,使用共享容器获取随机值,以确保相同的测试输入。我还按照从最长switch语句到最短语句的顺序运行了测试,反之亦然,以尝试消除与排序相关的测试问题的可能性。如果有人想尝试重现这些结果,我已将我的测试代码放在
github repo 上。

那么,这里发生了什么?我的架构或微基准构建的一些变幻莫测?还是 Java在to范围内switch的执行速度真的比up to快一点?18``28
case``11``17

github 测试仓库“切换实验”

更新: 我清理了基准测试库,并在 /results
中添加了一个文本文件,其中包含更广泛的可能exponent值的一些输出。我还在测试代码中添加了一个不抛出Exceptionfrom的选项default,但这似乎不会影响结果。

更新 2: 在 2009 年的 xkcd
论坛上发现了一些关于这个问题的很好的讨论:http
://forums.xkcd.com/viewtopic.php?f=11&t=33524 。OP 对 using
的讨论Array.binarySearch()让我想到了上面求幂模式的简单的基于数组的实现。不需要二进制搜索,因为我知道其中的条目是什么array。它的运行速度似乎比
using 快 3 倍switch,显然是以牺牲一些控制流为代价的switch。该代码也已添加到 github 存储库中。

.contiguous() 在 PyTorch 中做了什么?

.contiguous() 在 PyTorch 中做了什么?

x.contiguous()张量有什么作用x

答案1

小编典典

PyTorch 中有一些对张量的操作不会改变张量的内容,但会改变数据的组织方式。这些操作包括:

narrow(), view(),expand()transpose()

例如: 当您调用 时transpose(),PyTorch
不会生成具有新布局的新张量,它只是修改张量对象中的元信息,以便偏移量和步幅描述所需的新形状。在这个例子中,转置张量和原始张量共享相同的内存:

x = torch.randn(3,2)y = torch.transpose(x, 0, 1)x[0, 0] = 42print(y[0,0])# prints 42

这就是 连续 的概念出现的地方。在上面的例子中,x是连续的,但y不是因为它的内存布局与从头开始制作的相同形状的张量的内存布局不同。请注意,
“连续” 这个词有点误导,因为张量的内容并不是散布在不连贯的内存块周围。这里字节仍然分配在一块内存中,但元素的顺序不同!

当您调用 时contiguous(),它实际上会复制张量,使其元素在内存中的顺序与使用相同数据从头开始创建的顺序相同。

通常你不需要担心这个。您通常可以安全地假设一切都会正常工作,并等到您得到RuntimeError: input is notcontiguousPyTorch 期望连续张量添加对contiguous().

19.1.2 Switch Virtual Interface Configuration

19.1.2 Switch Virtual Interface Configuration

To access the switch remotely, an IP address and a subnet mask must be configured on the switch virtual interface (SVI). To configure an SVI on a switch, use the interface vlan 1 global configuration command. Vlan 1 is not an actual physical interface but a virtual one. Next, assign an IPv4 address using the ip address ip-address subnet-mask interface configuration command. Finally, enable the virtual interface using the no shutdown interface configuration command.

After the switch is configured with these commands, the switch has all the IPv4 elements ready for communication over the network.

Note: Similar to Windows hosts, switches configured with an IPv4 address will typically also need to have a default gateway assigned. This can be done using the ip default-gateway ip-address global configuration command. The ip-address parameter would be the IPv4 address of the local router on the network, as shown in the example. However, in this topic you will only be configuring a network with switches and hosts. Routers will be configured later.

525. Contiguous Array

525. Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

Example 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

Note: The length of the given binary array will not exceed 50,000.

public class Solution {
    public int findMaxLength(int[] nums) {
        if(nums == null || nums.length == 0){
            return 0;
        }
        for(int i=0; i<nums.length; i++){
            if(nums[i] == 0){
                nums[i] = -1; //先把所有的0变成-1,这样就是求sum为0的最大区间
            }
        }
        int sum = 0, max = 0;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(0, -1);
        for(int i=0; i<nums.length; i++){
            sum += nums[i];
            if(!map.containsKey(sum)){
                map.put(sum, i);
            }else{
                max = Math.max(max, i-map.get(sum));
            }
        }
        return max;
    }
}


994.Contiguous Array 邻近数组

994.Contiguous Array 邻近数组

描述

Given a binary array,find the maximum length of a contiguous subarray with equal number of 0 and 1.

示例

Example 1:

Input: [0,1]
Output: 2
Explanation: [0,1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0,1] (or [1,0]) is a longest contiguous subarray with equal number of 0 and 1.

Note: The length of the given binary array will not exceed 50,000.

给出二进制数组,输出连续的含有0、1个数相等的子数组的长度。
这里用到一个sum,遇到1就加1,遇到0就减1,这样就会得到每个角标下的sum。
哈希表建立sum值和角标之间的映射。
遍历num成员计算sum值,如果哈希表中存在该sum值,就用当前角标减去哈希表中sum对应的角标,就会得到中间子数组长度,比较更新res。如果哈希表中不存在则添加该sum值和对应的角标。

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int res = 0,n = nums.size(),sum = 0;
        unordered_map<int,int> m{{0,-1}};
        for (int i = 0; i < n; ++i) {
            sum += (nums[i] == 1) ? 1 : -1;
            if (m.count(sum)) {
                res = max(res,i - m[sum]);
            } else {
                m[sum] = i;
            }
        }
        return res;
    }
};

关于为什么 Java switch on contiguous ints 似乎在添加案例时运行得更快?switch例子java的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于.contiguous() 在 PyTorch 中做了什么?、19.1.2 Switch Virtual Interface Configuration、525. Contiguous Array、994.Contiguous Array 邻近数组的相关知识,请在本站寻找。

本文标签: