我需要在 C# 中给定区域大小来导出宽度和高度。宽度和高度需要是尽可能接近正方形的整数。例如 - 面积 = 100 --> 宽度 = 10,高度 = 10 面积 = 162 --> 面积 = 162 --> 宽度 = 18,高度 = 9。
我该怎么做?
考虑一些使用未初始化堆栈变量的非常简单的代码(或更复杂的代码,请参见下文1),例如:
int main() { int x; return 17 / x; }
Run Code Online (Sandbox Code Playgroud)
这是 GCC 发出的 ( -O3):
mov eax, 17
xor ecx, ecx
cdq
idiv ecx
ret
Run Code Online (Sandbox Code Playgroud)
以下是 MSVC 发出的内容 ( -O2):
mov eax, 17
cdq
idiv DWORD PTR [rsp]
ret 0
Run Code Online (Sandbox Code Playgroud)
作为参考,以下是 Clang 发出的内容 ( -O3):
ret
Run Code Online (Sandbox Code Playgroud)
问题是,所有三个编译器都检测到这是一个未初始化的变量就好了 ( -Wall),但实际上只有一个编译器基于它执行任何优化。
这让我有点难堪……我认为几十年来为未定义行为而争论的所有内容都是为了允许编译器优化,但我看到只有一个编译器关心优化甚至是最基本的 UB 情况。
为什么是这样?如果我想要Clang 以外的编译器优化 UB 的这种情况,我该怎么办?有什么方法可以让我真正获得 UB 的好处,而不仅仅是任何一个编译器的缺点?
1显然,对于某些人来说,这对于SSCCE来说太过分了,无法理解实际问题。如果你想要一个更复杂的这个问题的例子,在程序的每次执行中都不是未定义的,只需稍微按摩一下。例如:
int main(int argc, char …Run Code Online (Sandbox Code Playgroud) 我做了很多问题,比如:
SELECT THIS
FROM THAT
WHERE ROW_TIME < NOW()
Run Code Online (Sandbox Code Playgroud)
如何在PHP中创建包含当前时间的变量,以便我可以使其工作:
SELECT THIS
FROM THAT
WHERE ROW_TIME < '$variable'
Run Code Online (Sandbox Code Playgroud)
?
解决以下练习:
编写三个不同版本的程序来打印ia的元素.一个版本应该使用一个范围来管理迭代,另外两个应该在一个案例中使用普通的for循环使用下标,而在另一个案例中使用指针.在所有三个程序中直接编写所有类型.也就是说,不要使用类型别名,auto或decltype来简化代码.[C++ Primer]
一个问题出现了:这些访问阵列的方法在速度和原因方面都得到了优化?
我的解决方案
Foreach循环:
int ia[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
for (int (&i)[4]:ia) //1st method using for each loop
for(int j:i)
cout<<j<<" ";
Run Code Online (Sandbox Code Playgroud)嵌套for循环:
for (int i=0;i<3;i++) //2nd method normal for loop
for(int j=0;j<4;j++)
cout<<ia[i][j]<<" ";
Run Code Online (Sandbox Code Playgroud)使用指针:
int (*i)[4]=ia;
for(int t=0;t<3;i++,t++){ //3rd method. using pointers.
for(int x=0;x<4;x++)
cout<<(*i)[x]<<" ";
Run Code Online (Sandbox Code Playgroud)使用auto:
for(auto &i:ia) //4th one using auto but I think it is similar to 1st.
for(auto j:i)
cout<<j<<" ";
Run Code Online (Sandbox Code Playgroud)使用基准测试结果 clock()
1st: 3.6 (6,4,4,3,2,3)
2nd: 3.3 (6,3,4,2,3,2)
3rd: 3.1 (4,2,4,2,3,4) …Run Code Online (Sandbox Code Playgroud) 所以我想出了ProjectEuler问题29的这个解决方案(http://projecteuler.net/problem=29)
答案是对的.我希望这段代码运行得非常快,但运行速度非常慢.我不知道为什么.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<pair<int,int>> factorized_int; // pairs of base, exponent
factorized_int primeFactors(int n) {
int primeFactors[100] = {0};
for (int i=2; i <= n; i++) {
if (n%i == 0) {
primeFactors[i]++;
n /= i;
i--;
}
}
vector<pair<int,int>> retValue;
for (int i=2; i<100; i++) {
if (primeFactors[i] != 0) {
retValue.push_back(pair<int,int>(i,primeFactors[i]));
}
}
return retValue;
}
factorized_int pow(factorized_int n, int exponent) {
factorized_int retValue = factorized_int(n);
for (size_t …Run Code Online (Sandbox Code Playgroud) 我有一小段代码,每个代码都在使用.我已经优化了我的代码,但我的老板希望我进一步优化它.我不知道在这里可以做进一步的优化.
foreach (Match match in matches) {
//When oveeride is false and you have to download all the files
if (Override.Equals(false)) {
//putting the matches from regular expression into DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
//When override is true and a paticular file has to be downloaded
else if (match.Groups[2].Value.Equals(OverrideFileName)) {
//putting the matche from regular expression into a DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to …Run Code Online (Sandbox Code Playgroud) 我正在制作2D射击游戏,因此我必须在阵列中填充许多子弹,包括它们的位置,以及它们的位置.
所以我有两个问题,一个是内存使用,特别是编写的数组不会使事情不对齐,导致大量的填充或对齐,这使得计算的速度变得很糟糕.
第二是计算速度.
首先,这意味着选择整数或浮点数...现在我将使用整数(如果有人认为浮点数更好,请说出来).
那么,这也意味着选择那种类型的变体(8位?16位?C混淆默认值?CPU字大小?单精度?双精度?)
因此问题是:在现代处理器中C中哪种类型最快(即:常见的x86,ARM和其他流行的处理器,不用担心Z80或36位处理器),以及在使用速度和内存时使用哪种类型更合理帐户?
签名和未签名的速度有差异吗?
编辑是因为关闭投票:是的,它可能是过早优化,但我不仅要求CPU使用,而且要求内存使用(可能会有很大差异),我也在做项目来锻炼我的C技能,这是几年我不用C编码,我想有一些乐趣并找到限制并拉伸它们,并且还学习新标准(上次我使用C它仍然是C89).
最后,当我发现新的标准中存在一些新的有趣类型(如int_fast*_t)时,提出这个问题的主要动机就是黑客的好奇心.
但如果你仍然认为这不值得问,那么我可以删除这个问题并仔细阅读标准和一些书籍,自己学习.如果其他人有一天有同样的好奇心,那不是我的问题.
给定数字'n'和相应的二进制值.我想只使用'n'中设置的位来生成n的所有组合.
例如:如果n = 11且其二进制表示为1011,则组合为:
0000
0001
0010
0011
1000
1001
1010
1011
Run Code Online (Sandbox Code Playgroud)
例2:如果n = 49且其二进制表示为11001,则组合为:
00000
00001
01000
01001
10000
10001
11000
11001
Run Code Online (Sandbox Code Playgroud)
最简单的方法可能是编写一个C子程序来生成这些组合,但是,我需要一些有效的方法/算法来生成这些组合(一些位操作技术类似于bit twiddling hacks).
谢谢.
在JAVA中,我给我的对象提供了id号(int).我想比较给定的对象是否是预期的对象.
我应该使用哪个?哪个更快?
if(civ!=this)
Run Code Online (Sandbox Code Playgroud)
要么
if(civ.id!=id)
Run Code Online (Sandbox Code Playgroud)
编辑:
额外的信息:
Class Civ {
int id;
public Civ(int i){
id = i;
}
public boolean checkIfOther(Civ civ){
Run Code Online (Sandbox Code Playgroud)
这个:
return (civ.id !=id);
Run Code Online (Sandbox Code Playgroud)
或这个:
return(civ !=this);
Run Code Online (Sandbox Code Playgroud)
-
}
}
Run Code Online (Sandbox Code Playgroud) 许多测试用例都是超时的.我已经确定我在任何地方使用懒惰的评估,线性(或更好)的例程等等.我很震惊,这仍然不符合性能基准.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Mine
{
public int Distance { get; set; } // from river
public int Gold { get; set; } // in tons
}
class Solution
{
static void Main(String[] args)
{
// helper function for reading lines
Func<string, int[]> LineToIntArray = (line) => Array.ConvertAll(line.Split(' '), Int32.Parse);
int[] line1 = LineToIntArray(Console.ReadLine());
int N = line1[0], // # of mines
K = line1[1]; // # of pickup locations
// Populate mine info …Run Code Online (Sandbox Code Playgroud)