HTTPWebRequest 类有多快?

发布于 2025-01-01 00:10:50 字数 198 浏览 0 评论 0原文

我想知道,与自行编写的 HTTP-Response 解析器相比,HTTPWebRequest 的速度有多快。

我知道 HTTPWebRequest 类能够使用带有管道的持久 TCP 连接(默认情况下启用管道)。还可以设置缓存和压缩的值。

响应的解析可能永远不是瓶颈,但这只是出于我的好奇心:HTTPWebRequest 类是否会产生“不必要的”开销?

I was wondering, how fast the HTTPWebRequest is in comparision with a self-written parser for the HTTP-Response.

I know the HTTPWebRequest class is capable of using a persistent TCP connection with pipelining (pipelining is enabled by default). It's also possible to set values for caching and compression.

The parsing of the response is probably never the bottleneck, but just for my curiosity: Does the HTTPWebRequest class produce "unnecessary" overhead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌枕肩 2025-01-08 00:10:50

如果不知道自己编写的解析器是什么,就不可能对您的问题给出量化的答案。也就是说,您可能能够编写一个比HTTPWebRequest更快的解析器,如果(例如):

  • 您的响应将只包含一组特定的标头
  • 标头将仅按特定顺序返回
  • 您的请求将仅发送到一个目的地
  • 可用于限制将对响应执行的处理的任何其他约束

如果您可以限制场景,您或许能够超越HTTPWebRequest,因为它必须能够处理任何有效响应,而自定义解析器必须只能处理它期望的场景。也就是说,请记住,您所编写的内容将不是一个 true HTTP 响应解析器,而是处理有效响应的严格子集的东西。

判断是否存在可以避免的开销的唯一方法是编写自己的(子集)解析器并将其性能与内置解析器的性能进行比较。

Without knowing what the self-written parser is, it's not possible to give a quantified answer to your question. That said, you might be able to write a parser that is quicker than HTTPWebRequest, if (for example):

  • Your response will only ever contain a certain set of headers
  • The headers will only ever be returned in a certain order
  • Your request will only ever go to one destination
  • Any other constraints you can use to limit the processing that will be performed on the response

If you can constrain the scenario, you may be able to outperform HTTPWebRequest as it has to be able to handle any valid response, whereas a custom parser has to only be able to handle scenarios it's expecting. That said, bear in mind that what you're writing won't then be a true HTTP response parser, rather something that handles a strict sub-set of valid responses.

The only way you can tell if there's any overhead which you can avoid is by writing your own (sub-set) parser and comparing its performance to that of the in-built one.

清风挽心 2025-01-08 00:10:50

我没有结论,但我认为这类问题应该由实际案例来激发。例如,HttpWebRequest 类是否会减慢您的解决方案?您是否使用某些探查器检查了其性能影响?

毕竟,关于此类优化问题有一句众所周知的话:过早的优化是任何软件开发的魔鬼

我最好的建议是你应该关心其他事情,使用一个好的分析器并找出你的程序存在性能问题的地方(我确信 HttpWebRequest 不会成为问题!)。

I don't have a conclusion, but I believe this kind of questions should be motivated by an actual case scenario. For example, does HttpWebRequest class slowdowns your solution? Have you checked its performance impact using some profiler?

And, after all, there's a well-known sentence about this kind of questions regarding optimization: premature optimization is the devil of any software development.

My best suggestion is you should take care about other things, use a good profiler and find out where your program have performance issues (I'm sure HttpWebRequest won't be the problem!).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文