你怎么能改进这个功能? [英] how can you improve this function?

查看:84
本文介绍了你怎么能改进这个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我这里有一些代码,基本上在一个字符串中查找,

出现任何3个相同的字符串。所以这个函数会报告AAA

bbb等。我后来添加了一些代码

,这是检测相同char出现的情况所需的时间4

次,例如AAAA,在这种情况下想跳过剩下的3

chars并检查字符串中的下一个字符。这导致我在我的for循环中增加了我的for循环计数器,我知道这是

应该是不好的练习,你们有什么建议吗? br />

无论如何这里代码 - thx


private bool lookfor3CharacterOccurance(字符串行,字符串路径)

{

bool found = false;


for(int i = 0; i< = line.Length-4; i ++)

{


//对这些格式化字符不感兴趣

if(line [i]!=''''&&(line [i]! =''\ u000A'')&&(line [i]!=''\ u000B'')

&&(line [i]!=''\\ \\ u00C'')&&(line [i]!=''\ u000D'')&&(line [i]!=

''\ u2028'' )&&(line [i]!=''\ u2029'')&&(line [i]!=''\ u0009''))

{

//只有相同类型的3个字符的重复次数

if((line [i] == line [i + 1])& amp;&(line [i] == line [i + 2]))

{

if(!(line [i] == line [i + 3]))

{

//Console.WriteLine("{0} {1} {2}",line [i],line [i + 1],第[i + 2]行;

shhpErr.Add(" File:" +路径+包含至少一次出现的相同字符的次数为

:+ line [i] + line [i + 1] + line [i + 2]);

found = true;

break;

}

else

{

i = i + 3;

}

}

}

}


返回找到;

}

解决方案

< ph ***** @ vistatec.ie>在留言中写道

新闻:a3 ************************** @ posting.google.c om ...

这导致我在我的for循环中增加了我的for循环计数器,我知道这应该是不好的练习,
你们有什么建议吗? / blockquote>


这实际上是不好的做法,因为''for''对应于

C(和基于C语言)的成语,是一个数据结构

遍历(数组,列表等):为数据结构中的每个

元素执行给定任务。您可以阅读以下行:


for(int i = 0; i< array.length; i ++){...}


作为'数组中'每个元素'的C转换''做...。


''for'循环有时被错误地用于不要
对应那个成语。在这种情况下,''while''应该用
代替:


int i = 0;

while(i < array.length){

... //任务可能会根据内容的不同而变化

//数组

i ++;

}

-

Matthieu Villeneuve


菲利普,


除非你事先知道字符串的

结构(我相信你做的),否则你不能不跳过去不)。对于

的例子,当你遇到第一个''A'时,你不知道还有

三个,所以你必须跳到下一个并进行比较。


我会对你正在做的事采取不同的方法。我会有两个字典。
两个字典。而不是每次都向前看,为什么不跟踪扫描的最后四个字符中的



希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


< ph ***** @ vistatec.ie>在留言中写道

新闻:a3 ************************** @ posting.google.c om ...



我这里有一些代码,基本上在一个字符串中查找,
出现任何3个相同的字符串。所以这个函数会报告AAA
bbb等。我后来添加了一些代码来检测相同的char出现的情况,例如AAAA,在这种情况下想要跳过剩余的3个
字符并检查下一个字符在字符串中。这导致我在我的for循环中增加了我的for循环计数器,我知道这是不好的做法,你们有什么建议可以吗?

这里无论如何代码 - thx

私人bool lookfor3CharacterOccurance(字符串行,字符串路径)
{
bool found = false;

for(int i = 0; i< = line.Length-4; i ++)
//
//对这些格式化字符不感兴趣
if(line [i]!='''' &&(line [i]!=''\ u000A'')&&(line [i]!=''\ u000B'')
&&(line [i ]!=''\ u000C'')&&(line [i]!=''\ u000D'')&&(line [i]!=
''\ u2028 '')&&(line [i]!=''\ u2029'')&&(line [i]!=''\ u0009''))
{
//只有相同类型的3个字符的重复次数
if((line [i] == line [i + 1])&&(line [i] == line [i + 2] ))
{
if(!(line [i] == line [ i + 3])
{
//Console.WriteLine(" {0} {1} {2}",line [i],line [i + 1],line [i +2]);
shhpErr.Add(" File:" +路径+至少包含一次相同的字符出现3次:+ line [i] + line [i + 1] + line [i + 2]);
found = true;
休息;
}

{
i = i + 3;
}
}
}
}

返回发现;
}



保留一个统计连续相同字符的整数,如:


int count = 0;

for(int i = 0; i< line.length-1; i ++)

{

if(line [i] == line [i + 1])

count ++;

else

if(数量)

{

if(count> 3)...

if(count = 3)...

count = 0;

}

}

if(count> 3)...

if(count = 3)...


您可能想将if短语放入另一个函数中。


那样你代码不需要7 A'代表4 A'(已被跳过)加3

A'(不是)

>
你也可以将循环计数器增加2,仅在(line [i] == line [i + 1])命中时检查(line [i] ==

line [i-1])因为你不感兴趣

in two-couples),但我怀疑这会改善性能,因为问题

可能是内存/缓存绑定处理器等待大部分时间数据

时间。


Niki


< ph ***** @ vistatec.ie> schrieb im Newsbeitrag

新闻:a3 ************************** @ posting.google.c om ...



我这里有一些代码,基本上在一个字符串中查找,
出现任何3个相同的字符串。所以这个函数会报告AAA
bbb等。我后来添加了一些代码来检测相同的char出现的情况,例如AAAA,在这种情况下想要跳过剩余的3个
字符并检查下一个字符在字符串中。这导致我在我的for循环中增加了我的for循环计数器,我知道这是不好的做法,你们有什么建议可以吗?

这里无论如何代码 - thx

私人bool lookfor3CharacterOccurance(字符串行,字符串路径)
{
bool found = false;

for(int i = 0; i< = line.Length-4; i ++)
//
//对这些格式化字符不感兴趣
if(line [i]!='''' &&(line [i]!=''\ u000A'')&&(line [i]!=''\ u000B'')
&&(line [i ]!=''\ u000C'')&&(line [i]!=''\ u000D'')&&(line [i]!=
''\ u2028 '')&&(line [i]!=''\ u2029'')&&(line [i]!=''\ u0009''))
{
//只有相同类型的3个字符的重复次数
if((line [i] == line [i + 1])&&(line [i] == line [i + 2] ))
{
if(!(line [i] == line [ i + 3])
{
//Console.WriteLine(" {0} {1} {2}",line [i],line [i + 1],line [i +2]);
shhpErr.Add(" File:" +路径+至少包含一次相同的字符出现3次:+ line [i] + line [i + 1] + line [i + 2]);
found = true;
休息;
}

{
i = i + 3;
}
}
}
}

返回发现;
}



hi,

I have some code here which basically look for within a string, the
occurance of any 3 consectative characters which are the same. so AAA
bbb etc would be reported by this function. I later added some code
which is needed to detect the scenario where the same char appears 4
times for example AAAA, in this case want to skip the remaining 3
chars and check the next character in the string. This has led me to
increase my for loop counter within my for loop, I know this is
supposed to be bad practice, is there anything you guys can suggest?

Here the code anyway - thx

private bool lookfor3CharacterOccurance(string line, string path)
{
bool found = false;

for(int i=0;i<=line.Length-4;i++)
{

//not interested in these formatting characters
if(line[i] != '' '' && (line[i] != ''\u000A'') && (line[i] != ''\u000B'')
&& (line[i] != ''\u000C'') && (line[i] != ''\u000D'') && (line[i] !=
''\u2028'') && (line[i] != ''\u2029'') && (line[i] != ''\u0009''))
{
//only what reoccurances of 3 characters of the same type
if((line[i] == line[i+1] ) && (line[i] == line[i+2] ) )
{
if(!(line[i] == line[i+3]))
{
//Console.WriteLine("{0} {1} {2}",line[i],line[i+1],line[i+2]);
shhpErr.Add("File: " + path + " contains at least one occurances of
the same characters 3 times: "+line[i]+line[i+1]+line[i+2]);
found = true;
break;
}
else
{
i=i+3;
}
}
}
}

return found;
}

解决方案

<ph*****@vistatec.ie> wrote in message
news:a3**************************@posting.google.c om...

This has led me to increase my for loop counter within
my for loop, I know this is supposed to be bad practice,
is there anything you guys can suggest?



This is actually bad practice, because ''for'' corresponds to
an idiom in C (and C-based languages), which is a data structure
traversal (array, list, etc.): perform a given task for every
element in the data structure. You can read the following line:

for (int i = 0; i < array.length; i++) { ... }

as the C translation of "for every element in ''array'' do ...".

''for'' loops are sometimes wrongly used for situations that do not
correspond to that idiom. In that case, ''while'' should be used
instead:

int i = 0;
while (i < array.length) {
... // a task that may vary according to the contents
// of the array
i++;
}
--
Matthieu Villeneuve


Philip,

You are not going to be able to NOT skip ahead, unless you know the
structure of the string beforehand (which I am sure you do not). For
example, when you come across the first ''A'', you don''t know that there are
three more, so you have to skip to the next one and compare it.

I would take a different approach to what you are doing. I would have
two dictionaries. Instead of looking forward each time, why not keep track
of the last four characters scanned?

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ph*****@vistatec.ie> wrote in message
news:a3**************************@posting.google.c om...

hi,

I have some code here which basically look for within a string, the
occurance of any 3 consectative characters which are the same. so AAA
bbb etc would be reported by this function. I later added some code
which is needed to detect the scenario where the same char appears 4
times for example AAAA, in this case want to skip the remaining 3
chars and check the next character in the string. This has led me to
increase my for loop counter within my for loop, I know this is
supposed to be bad practice, is there anything you guys can suggest?

Here the code anyway - thx

private bool lookfor3CharacterOccurance(string line, string path)
{
bool found = false;

for(int i=0;i<=line.Length-4;i++)
{

//not interested in these formatting characters
if(line[i] != '' '' && (line[i] != ''\u000A'') && (line[i] != ''\u000B'')
&& (line[i] != ''\u000C'') && (line[i] != ''\u000D'') && (line[i] !=
''\u2028'') && (line[i] != ''\u2029'') && (line[i] != ''\u0009''))
{
//only what reoccurances of 3 characters of the same type
if((line[i] == line[i+1] ) && (line[i] == line[i+2] ) )
{
if(!(line[i] == line[i+3]))
{
//Console.WriteLine("{0} {1} {2}",line[i],line[i+1],line[i+2]);
shhpErr.Add("File: " + path + " contains at least one occurances of
the same characters 3 times: "+line[i]+line[i+1]+line[i+2]);
found = true;
break;
}
else
{
i=i+3;
}
}
}
}

return found;
}



keep an integer that counts "consecutive identical characters", like:

int count = 0;
for (int i=0; i<line.length-1; i++)
{
if (line[i] == line[i+1])
count++;
else
if (count)
{
if (count > 3) ...
if (count = 3) ...
count = 0;
}
}
if (count > 3) ...
if (count = 3) ...

You might want to put the if phrases into another function.

That way you code doesn''t take 7 A''s for 4 A''s (which are skipped) plus 3
A''s (which aren''t)

You could also increase the loop counter by two, checking (line[i] ==
line[i-1]) only if (line[i] == line[i+1]) hit (since you''re not interested
in 2-couples), but I doubt this will improve performance, since the problem
is probably memory/cache-bound with the processor waiting for data most of
the time.

Niki

<ph*****@vistatec.ie> schrieb im Newsbeitrag
news:a3**************************@posting.google.c om...

hi,

I have some code here which basically look for within a string, the
occurance of any 3 consectative characters which are the same. so AAA
bbb etc would be reported by this function. I later added some code
which is needed to detect the scenario where the same char appears 4
times for example AAAA, in this case want to skip the remaining 3
chars and check the next character in the string. This has led me to
increase my for loop counter within my for loop, I know this is
supposed to be bad practice, is there anything you guys can suggest?

Here the code anyway - thx

private bool lookfor3CharacterOccurance(string line, string path)
{
bool found = false;

for(int i=0;i<=line.Length-4;i++)
{

//not interested in these formatting characters
if(line[i] != '' '' && (line[i] != ''\u000A'') && (line[i] != ''\u000B'')
&& (line[i] != ''\u000C'') && (line[i] != ''\u000D'') && (line[i] !=
''\u2028'') && (line[i] != ''\u2029'') && (line[i] != ''\u0009''))
{
//only what reoccurances of 3 characters of the same type
if((line[i] == line[i+1] ) && (line[i] == line[i+2] ) )
{
if(!(line[i] == line[i+3]))
{
//Console.WriteLine("{0} {1} {2}",line[i],line[i+1],line[i+2]);
shhpErr.Add("File: " + path + " contains at least one occurances of
the same characters 3 times: "+line[i]+line[i+1]+line[i+2]);
found = true;
break;
}
else
{
i=i+3;
}
}
}
}

return found;
}



这篇关于你怎么能改进这个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆