我试图迭代一个字符串,以删除重复字符.
例如,字符串aabbccdef应该成为abcdef
,字符串abcdabcd应该成为abcd
这是我到目前为止:
public class test {
public static void main(String[] args) {
String input = new String("abbc");
String output = new String();
for (int i = 0; i < input.length(); i++) {
for (int j = 0; j < output.length(); j++) {
if (input.charAt(i) != output.charAt(j)) {
output = output + input.charAt(i);
}
}
}
System.out.println(output);
}
}
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
我正在搜索关于"C程序如何工作"的博客文章一个月.大多数人都喜欢
我想我会先阅读有关编译器如何理解程序流入机器的工作原理.龙书似乎是普遍的首选.但说实话,这太过密集了.我现在还不够好,无法完成所有这些工作.
所以我开始阅读有关硬件的内容.但是,他们也解释了总线,I/O信号,内存结构,编写缓存友好代码等等.但没有适当的例子.
但我仍然无法让自己满意或能够完全想象这个过程.
2个小时前,我决定提出这个问题.(因为我害怕它可能对SO社区,或题外话题或其他可下注的类别没用),而且我没有找到任何与此相关的帖子.有一个关于"编译器如何编译",但答案表明这是一个太宽泛的问题.
我的问题是:
我想知道C程序的工作原理.如果您无法明确告诉我,请将我重定向到另一个网站上的书或其他帖子,以便给我答案.
我在这里,直到得到回复.如果您对此帖有任何建议,请告诉我.这不是我的第一语言,所以请把我所有的句子都当作柔软和礼貌的.
谢谢.
更新:
除了接受的答案外,还有一些非常好的链接以及建议,可以给出部分答案或进一步了解我想要了解的内容.
我试图之间作出选择map,并unordered_map为下面的用例:
关键map是指针.最常见的用例是地图中将有一个元素.通常,地图中的最大元素数小于10.地图经常被访问,速度是最重要的因素.对地图的更改很少发生.
虽然测量速度显然是正确的方法,但这个代码将在几个平台上使用,所以我试图创建一个通用的经验法则,用于在a map和unordered_map基于元素的数量之间进行选择.我在这里看到一些帖子暗示std :: map对于少数元素可能更快,但没有给出"小"的定义.
是否有一个经验法则可以选择a map和unordered_map基于元素的数量?另一种数据结构(如通过线性搜索vector)更好吗?
我正在用VB.NET 2010编写一个计算密集型程序,我希望优化速度.如果操作的结果被分配给类级变量,我发现运算符AndAlso并且OrElse异常缓慢.例如,虽然声明
a = _b AndAlso _c
_a = a
Run Code Online (Sandbox Code Playgroud)
在编译的exe中,它们之间需要大约6个机器周期,单个语句
_a = _b AndAlso _c
Run Code Online (Sandbox Code Playgroud)
大约需要80个机器周期.这里_a,_b并且_c是私有布尔变量Form1,并且所讨论的语句在实例过程中Form1,其中a是一个局部布尔变量.
我无法找到为什么单一陈述需要这么长时间.我已经使用NetReflector探索了它,直到CIL代码的水平,看起来很好:
Instruction Explanation Stack
00: ldarg.0 Push Me (ref to current inst of Form1) Me
01: ldarg.0 Push Me Me, Me
02: ldfld bool Form1::_b Pop Me, read _b and push it _b, Me
07: brfalse.s 11 Pop _b; if false, branch to 11 Me
09: ldarg.0 …Run Code Online (Sandbox Code Playgroud) 为什么y[i] < x[i]当数组x的值总是高于y(对于ex 1<x<2,和0<y<1)时,函数需要两倍的时间.另外,比较时0.5<x<1.5,0<y<1执行时间约为1.5倍的情况0<x<1,和0<y<1.这假设x和y都是长数组.
我为你添加了代码,试着去理解我的意思.您可以通过增加和减少变量"offset(try offset = 1和offset = 0)来偏移数组x;代码将在文件Beta中存储循环的执行时间.
代码是:
#include <iostream>
#include <array>
#include <time.h>
#include <math.h>
using namespace std;
#define MAX(x,y) ((x) > (y) ? (x) : (y))
int main()
{
ofstream myfile_Beta;
myfile_Beta.open ("Beta.txt");
clock_t begin_time = clock();
clock_t total_time;
srand (time(NULL));
double offset =0.0;
int m=0;
for(int k=0;k<10000;k++)
{
m=1;
double M[75720],x[75720],y[75720];
for (int i=0;i<75720;i++)
{
x[i]=+(rand()%1024)/1024.0* 1.0 + offset ;
y[i]=+(rand()%1024)/1024.0* …Run Code Online (Sandbox Code Playgroud) 我在不同的项目中使用了这两个编译器.
它们在代码处理和输出生成方面有何不同?例如两者gcc并clang具有-O2优化的选项.它们在优化代码方面是否以相同的方式运行(高级别)?我做了一点测试,例如,如果我有以下代码:
int foo(int num) {
if(num % 2 == 1)
return num * num;
else
return num * num +1;
}
Run Code Online (Sandbox Code Playgroud)
以下是带有-ng的clang和gcc的输出程序集:
----gcc 5.3.0----- ----clang 3.8.0----
foo(int): foo(int):
movl %edi, %edx movl %edi, %eax
shrl $31, %edx shrl $31, %eax
leal (%rdi,%rdx), %eax addl %edi, %eax
andl $1, %eax andl $-2, %eax
subl %edx, %eax movl %edi, %ecx
cmpl $1, %eax subl %eax, %ecx
je .L5 imull %edi, %edi
imull %edi, %edi cmpl …Run Code Online (Sandbox Code Playgroud) 在这篇文章中,为什么处理排序数组的速度比随机数组快,它说分支预测是排序数组性能提升的原因.
但我只是尝试使用Python的例子; 我认为排序和随机数组之间没有区别(我尝试了bytearray和数组;并使用line_profile来分析计算).
我错过了什么吗?
这是我的代码:
from array import array
import random
array_size = 1024
loop_cnt = 1000
# I also tried 'array', and it's almost the same
a = bytearray(array_size)
for i in xrange(array_size):
a.append(random.randint(0, 255))
#sorted
a = sorted(a)
@profile
def computation():
sum = 0
for i in xrange(loop_cnt):
for j in xrange(size):
if a[j] >= 128:
sum += a[j]
computation()
print 'done'
Run Code Online (Sandbox Code Playgroud) 考虑以下测试程序:
int main( void ) {
int iterations = 1000000000;
while ( iterations > 0 )
-- iterations;
}
Run Code Online (Sandbox Code Playgroud)
int main( void ) {
int iterations = 1000000000;
int * p = & iterations;
while ( * p > 0 )
-- * p;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdlib.h>
int main( void ) {
int * p = malloc( sizeof( int ) );
* p = 1000000000;
while ( *p > 0 )
-- * p;
}
Run Code Online (Sandbox Code Playgroud)
通过使用-O0编译它们,我得到以下执行时间:
case1.c …Run Code Online (Sandbox Code Playgroud) 这个:
if (var) {
var = false;
}
Run Code Online (Sandbox Code Playgroud)
对此:
var = false;
Run Code Online (Sandbox Code Playgroud)
有速度差吗?
我偶然发现了一些"奇怪的行为".我使用F#interactive来测试一些代码并编写
Seq.zip "ACT" "GGA" |> Seq.map ((<||) compare)
// val it : seq<int> = seq [-1; -1; 1]
Run Code Online (Sandbox Code Playgroud)
然后我想用它制作一个函数并写下来
let compute xs ys = Seq.zip xs ys |> Seq.map ((<||) compare)
// val compute : xs:seq<'a> -> xs:seq<'a> -> seq<int> when 'a : comparison
Run Code Online (Sandbox Code Playgroud)
这推广了第一段代码,我认为这是件好事......直到我尝试使用它
compute "ACT" "GGA"
// val it : seq<int> = seq [-6; -4; 19]
Run Code Online (Sandbox Code Playgroud)
因此,compare当存在不同的"观点"(显式类型与泛型)时,以某种方式对"相同的事物"采取不同的行为
我知道如何解决它:要么通过明确的类型
let compute (xs: #seq<char>) // ... or char seq or string
Run Code Online (Sandbox Code Playgroud)
或者保持类型通用并与sign函数组合
let compute (* ... …Run Code Online (Sandbox Code Playgroud) c ×3
performance ×3
c++ ×2
java ×2
c++11 ×1
clang ×1
comparison ×1
f# ×1
gcc ×1
generics ×1
optimization ×1
python ×1
std ×1
string ×1
vb.net ×1