我注意到了
int i=10000000;
boolean isPrime= false;
while(!isPrime){
i++;
System.out.println(item); //this kills performance
isPrime = checkIfPrime(i);
}
}
Run Code Online (Sandbox Code Playgroud)
打印变量的当前值会导致性能下降.我想偶尔打印一次,但要保持低操作的成本.
如何比较打印到屏幕的成本和计算?是否有任何技巧可以最大限度地降低这种成本[我应该打印10条记录中的一条,还是会因为条件检查而花费这么多?]
我为什么需要这个?好吧,我正在用Java做一些有趣的事情(例如"找到欧拉的结合的反例...... 27 ^ 5 + 84 ^ 5 + 110 ^ 5 + 133 ^ 5 = 144 ^ 5(Lander&Parkin,1966)", ).我想写一个既正确又快速的程序(这个反例是在60年代发现的,所以我应该能够在合理的时间内完成).在调试时我想拥有尽可能多的信息和可能,我想尽快找到反例.我最好的办法是什么?打印每个案例?- 太慢了.让它一夜之间运行?如果我错过了一些i++怎么办?
我有一个紧密的循环来搜索coprimes.一份清单primeFactors.它的第n个元素包含n的素数分解的排序列表.如果我检查c,并d使用coprimescheckIfPrimes
boolean checkIfPrimes(int c, int d, List<List<Integer>> primeFactors) {
List<Integer> common = new ArrayList<>(primeFactors.get(d)); //slow
common.retainAll(primeFactors.get(c));
return (common.isEmpty());
}
Run Code Online (Sandbox Code Playgroud)
primeFactors.get(d).retainAll(primeFactors.get(c))看起来很有希望,但它会改变我的可重用primeFactors对象.
创建新对象相对较慢.有没有办法加快这一步?我可以以某种方式利用列表排序的事实吗?我应该使用数组吗?
我可以使用类似的代码在 react-bootstrap 中添加一个带有文本的按钮
<Button
className={styles.feedbackButtonRating}
variant={this.state.liked ? 'success' : 'light'}
onClick={this.handleOnLikeClick}
>
{FEEDBACK_LIKE_BUTTON_TEXT}
</Button>
Run Code Online (Sandbox Code Playgroud)
有没有办法{FEEDBACK_LIKE_BUTTON_TEXT}用拇指向上图标替换文本?
我想得到一个数字的剩余四次幂.这是我的代码:
static int testMod(int a, int mod) {
/* //This looks clear
BigInteger a4 = a;
return (a4.pow(4))%mod;
*/
//This works
String a2String = Integer.toString(a);
String mod2String = Integer.toString(mod);
BigInteger a4 = new BigInteger(a2String);
BigInteger modBigInt = new BigInteger(mod2String);
a4 = a4.pow(4);
return a4.remainder(modBigInt).intValue();
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但转换为String似乎是不必要的,使用%运算符将比更简洁a.remainder(b).是否可以重写它以使其更清晰?
我想的范围减少arr到一个do ... while循环,所以一旦退出循环,可以销毁.
如果我宣布arr 内的do while循环,我得到的错误:
符号无法找到
我可以在循环之前声明它,但是arr即使我不再需要它,我仍然会在内存中保持膨胀.
//int[] arr = {0}; // this works, but the scope is outside the loop
do {
int[] arr = {0}; // causes "cannot find symbol" on `while` line
arr = evolve(arr); // modifies array, saves relevant results elsewhere
} while (arr.length < 9999);
Run Code Online (Sandbox Code Playgroud)
处理这种情况的正确方法是什么?
这是代码的最小示例。curl 请求curl http://127.0.0.1:5000/get_zip/my_zip.zip -o my_zip.zip应该向用户发送文件archive/my_zip.zip
gunicorn当服务器启动时,它可以在没有gunicorn.
from os import path
from flask import Flask, request, jsonify, json, send_file
app = Flask(__name__)
@app.route('/get_zip/<file_path>', methods=['GET'])
def get_zip(file_path): # file_path: path to a large zip file
return send_file(path.join('archive', file_path), as_attachment=True)
if __name__ == '__main__':
app.run(host="0.0.0.0", port="5000", debug=False, use_reloader=False)
Run Code Online (Sandbox Code Playgroud)
在gunicorn下运行时修复此断开连接的正确方法是什么?
我试图模仿一个用 1 填充的圆圈,以及用数组中的 0 填充数组的其余部分numpy。这与图形与圆重叠的数值计算有关。现在我最多可以按元素进行(这很慢):
import numpy as np
from my_magic_constants import center, radius
c1 = np.zeros((center*2, center*2))
for h in range(center*2):
for w in range(center*2):
if (center-h)**2 + (center-w)**2 < radius**2:
c1[h,w] = 1
Run Code Online (Sandbox Code Playgroud)
在 numpy 数组中初始化圆的更有效方法是什么?
更新
我只检查了最早答案的加速度。如果有人想对时间进行基准测试,这里是代码
import numpy as np
from time import time
eps = 1e-10
center = 8119
square_half_size = 5741
square = np.zeros((center*2, center*2))
square[center-square_half_size:center+square_half_size, center-square_half_size:center+square_half_size] = 1
def iou(c):
overlap = c * square
union = (c + square).clip(0,1)
return …Run Code Online (Sandbox Code Playgroud) 我有一个膨胀的JDialog类(~2000行),显示两个不相关的JTable.我想将它分成三个类(JDialog,Jtable1和JTable2).我可以研究每个表使用哪些变量和哪些方法并将它们移动到相关的类,但是这种手动重构将是乏味的.
有没有办法自动化这种重构?
要实现这一点,脚本应该有一个令牌累加器.第一个标记是,例如jTable2来自panel.add(jTable2).现在检查所有包含jTable2的行,并将标记添加到累加器.重复搜索相关的令牌,直到找不到新的令牌.现在为每个令牌找到包含它的行.展开选择以包括括号.
很难相信可论证的最大语言的程序员还没有创建这样的工具.这应该与IDE中的find usages工具非常相似.
java ×5
performance ×2
biginteger ×1
do-while ×1
flask ×1
geometry ×1
gunicorn ×1
javascript ×1
loops ×1
new-operator ×1
numpy ×1
python ×1
python-3.x ×1
reactjs ×1
refactoring ×1
scope ×1