以下哪种技术是将整数除以2的最佳选择,为什么?
技巧1:
x = x >> 1;
Run Code Online (Sandbox Code Playgroud)
技术2:
x = x / 2;
Run Code Online (Sandbox Code Playgroud)
这x是一个整数.
我们必须一直为日志输出构建字符串等等.在JDK版本中,我们学会了何时使用StringBuffer(许多追加,线程安全)和StringBuilder(许多追加,非线程安全).
有什么建议使用String.format()?它是否有效,或者我们是否被迫坚持连接性能很重要的单线?
例如丑陋的旧式,
String s = "What do you get if you multiply " + varSix + " by " + varNine + "?";
Run Code Online (Sandbox Code Playgroud)
与整洁的新风格(可能很慢),
String s = String.format("What do you get if you multiply %d by %d?", varSix, varNine);
Run Code Online (Sandbox Code Playgroud)
注意:我的具体用例是我的代码中的数百个"单行"日志字符串.它们不涉及循环,所以StringBuilder太重量级了.我String.format()特别感兴趣.
java string performance string-formatting micro-optimization
以下所有说明都做同样的事情:设置%eax为零.哪种方式最佳(需要最少的机器周期)?
xorl %eax, %eax
mov $0, %eax
andl $0, %eax
Run Code Online (Sandbox Code Playgroud) 我有一个性能关键的二元决策树,我想把这个问题集中在一行代码上.下面是二叉树迭代器的代码,其中包含针对它运行性能分析的结果.
public ScTreeNode GetNodeForState(int rootIndex, float[] inputs)
{
0.2% ScTreeNode node = RootNodes[rootIndex].TreeNode;
24.6% while (node.BranchData != null)
{
0.2% BranchNodeData b = node.BranchData;
0.5% node = b.Child2;
12.8% if (inputs[b.SplitInputIndex] <= b.SplitValue)
0.8% node = b.Child1;
}
0.4% return node;
}
Run Code Online (Sandbox Code Playgroud)
BranchData是一个字段,而不是属性.我这样做是为了防止它被内联的风险.
BranchNodeData类如下:
public sealed class BranchNodeData
{
/// <summary>
/// The index of the data item in the input array on which we need to split
/// </summary>
internal int SplitInputIndex = 0;
/// <summary>
/// The …Run Code Online (Sandbox Code Playgroud) 这个问题仅供我使用,因为我总是喜欢编写优化的代码,这些代码也可以在便宜的慢速服务器(或具有大量流量的服务器)上运行
我环顾四周,无法找到答案.我想知道这两个例子之间的速度有多快,记住我的情况下数组的键并不重要(伪代码自然):
<?php
$a = array();
while($new_val = 'get over 100k email addresses already lowercased'){
if(!in_array($new_val, $a){
$a[] = $new_val;
//do other stuff
}
}
?>
<?php
$a = array();
while($new_val = 'get over 100k email addresses already lowercased'){
if(!isset($a[$new_val]){
$a[$new_val] = true;
//do other stuff
}
}
?>
Run Code Online (Sandbox Code Playgroud)
由于问题的关键不在于数组冲突,我想补充一点,如果你害怕碰撞插入$a[$new_value],你可以使用$a[md5($new_value)].它仍然可能导致冲突,但是当从用户提供的文件中读取时会从可能的DoS攻击中消失(http://nikic.github.com/2011/12/28/Supercolliding-a-PHP-array.html)
我一直试图通过循环展开来优化一些极其性能关键的代码(一种快速排序算法,在蒙特卡罗模拟中被称为数百万次).这是我试图加速的内循环:
// Search for elements to swap.
while(myArray[++index1] < pivot) {}
while(pivot < myArray[--index2]) {}
Run Code Online (Sandbox Code Playgroud)
我尝试展开类似的东西:
while(true) {
if(myArray[++index1] < pivot) break;
if(myArray[++index1] < pivot) break;
// More unrolling
}
while(true) {
if(pivot < myArray[--index2]) break;
if(pivot < myArray[--index2]) break;
// More unrolling
}
Run Code Online (Sandbox Code Playgroud)
这完全没有区别所以我把它改成了更易读的形式.我曾经尝试过循环展开,但我有类似的经历.鉴于现代硬件上的分支预测器的质量,何时(如果有的话)循环展开仍然是一个有用的优化?
language-agnostic optimization performance micro-optimization
一位同事向我展示了我认为没有必要的代码,但果然,确实如此。我希望大多数编译器会将所有这三种相等性测试的尝试视为等效的:
#include <cstdint>
#include <cstring>
struct Point {
std::int32_t x, y;
};
[[nodiscard]]
bool naiveEqual(const Point &a, const Point &b) {
return a.x == b.x && a.y == b.y;
}
[[nodiscard]]
bool optimizedEqual(const Point &a, const Point &b) {
// Why can't the compiler produce the same assembly in naiveEqual as it does here?
std::uint64_t ai, bi;
static_assert(sizeof(Point) == sizeof(ai));
std::memcpy(&ai, &a, sizeof(Point));
std::memcpy(&bi, &b, sizeof(Point));
return ai == bi;
}
[[nodiscard]]
bool optimizedEqual2(const Point &a, const Point &b) { …Run Code Online (Sandbox Code Playgroud) 在另一个问题中,接受的答案建议用try/except块替换Python代码中的(非常便宜的)if语句以提高性能.
抛开编码样式问题,并假设从未触发异常,与异常处理程序相比,具有异常处理程序(与性能相比)有多大差异,而不是具有比较为零的if语句?
从StringJava中删除所有不可打印字符的最快方法是什么?
到目前为止,我已经尝试并测量了138字节,131个字符的字符串:
replaceAll()- 最慢的方法
replaceAll()
codepointAt()逐个获取代码点并附加到StringBuffer
charAt()逐个获取字符并附加到StringBuffer
char[]缓冲区,charAt()逐个获取字符并填充此缓冲区,然后转换回String
char[]缓冲区 - 旧的和新的,一次获取现有String的所有字符,getChars()逐个迭代旧缓冲区并填充新缓冲区,然后将新缓冲区转换为String - 我自己的最快版本
byte[],getBytes()并指定编码为"utf-8"
byte[]缓冲区的相同内容,但将编码指定为常量Charset.forName("utf-8")
byte[]缓冲区相同的东西,但指定编码为1字节本地编码(几乎没有理智的事情要做)
我最好的尝试如下:
char[] oldChars = new char[s.length()];
s.getChars(0, s.length(), oldChars, 0);
char[] newChars = new char[s.length()];
int newLen = 0;
for (int j = 0; j < s.length(); …Run Code Online (Sandbox Code Playgroud)