如何优化此代码段?
string page = wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB");
int pos;
while ((pos = page.IndexOf("/watch?v=")) > 0) {
page = page.Substring(pos);
page = page.Substring(page.IndexOf("video-time"));
page = page.Substring(page.IndexOf("aria-label"));
page = page.Substring(page.IndexOf(@"\u003e") + 6);
string vt = page.Substring(0, page.IndexOf(@"\u003c"));
page = page.Substring(page.IndexOf("title=") + 1);
page = page.Substring(page.IndexOf("title=") + 1);
page = page.Substring(page.IndexOf("title=") + 1);
page = page.Substring(page.IndexOf("\\\"") + 2);
string tt = page.Substring(0, page.IndexOf("\\\" aria-describedby="));
}
可悲的是,我不能跳过一些Substring
行,因为这似乎是找到vt
和tt
正确出现的唯一方法。
由于Substring
始终返回一个新字符串,因此我尝试使用StringBuilder
:
System.Text.StringBuilder sb=new System.Text.StringBuilder(wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB"));
int pos;
while ((pos = sb.ToString().IndexOf("/watch?v=")) > 0) {
sb.Remove(0,pos);
sb.Remove(0,sb.ToString().IndexOf("video-time"));
sb.Remove(0,sb.ToString().IndexOf("aria-label"));
sb.Remove(0,sb.ToString().IndexOf(@"\u003e") + 6);
string vt =sb.ToString(0,sb.ToString().IndexOf(@"\u003c"));
sb.Remove(0,sb.ToString().IndexOf("title=") + 1);
sb.Remove(0,sb.ToString().IndexOf("title=") + 1);
sb.Remove(0,sb.ToString().IndexOf("title=") + 1);
sb.Remove(0,sb.ToString().IndexOf("\\\"") + 2);
string tt =sb.ToString(0,sb.ToString().IndexOf("\\\" aria-describedby="));
}
我很惊讶地发现这个解决方案虽然看起来并不像ToString()
所有,但确实稍快一点。
现在,有没有办法进一步优化这个?也许甚至让它看起来更好?
答案 0 :(得分:0)
正如@harold所建议的
string page = wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB");
int pos;
while ((pos = page.IndexOf("/watch?v=")) > 0) {
int subPos=pos;
subPos=page.IndexOf("video-time",subPos);
subPos=page.IndexOf("aria-label",subPos);
subPos=page.IndexOf(@"\u003e",subPos);
subPos+=6;
string vt=page.Substring(subPos,(subPos=page.IndexOf(@"\u003c",subPos)));
subPos=page.IndexOf("title=",subPos);
subPos++;
subPos=page.IndexOf("title=",subPos);
subPos++;
subPos=page.IndexOf("title=",subPos);
subPos=page.IndexOf("\\\"",subPos);
subPos+=2;
string tt=page.Substring(subPos,(subPos=page.IndexOf("\\\" aria-describedby=", subPos)));
page=page.Substring(subPos);
}
似乎比使用StringBuilder
ToString()
要快得多
答案 1 :(得分:0)
根据@dbc的建议,使用these extension methods
System.Text.StringBuilder sb=new System.Text.StringBuilder(wc.DownloadString("https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgI8EhhVQ2ZXdHFQeUJNR183aTMzT2VlTnNaWncaIEVnWjJhV1JsYjNNZ0FEQUJPQUZnQVdvQWVnRTB1QUVB"));
int pos;
while ((pos = sb.ToString().IndexOf("/watch?v=")) > 0) {
sb.Remove(0,pos);
sb.Remove(0,sb.IndexOf("video-time"));
sb.Remove(0,sb.IndexOf("aria-label"));
sb.Remove(0,sb.IndexOf(@"\u003e") + 6);
string vt =sb.ToString(0,sb.IndexOf(@"\u003c"));
sb.Remove(0,sb.IndexOf("title=") + 1);
sb.Remove(0,sb.IndexOf("title=") + 1);
sb.Remove(0,sb.IndexOf("title=") + 1);
sb.Remove(0,sb.IndexOf("\\\"") + 2);
string tt =sb.ToString(0,sb.IndexOf("\\\" aria-describedby="));
}
应该比使用StringBuilder
使用ToString()
更快,我的测试结果有点不稳定。