【问题标题】:Implementing logic on comparing two strings before processing further在进一步处理之前实现比较两个字符串的逻辑
【发布时间】:2020-01-09 01:07:17
【问题描述】:

我正在尝试将一些逻辑合并到一个应用程序中,以提高其性能,但我根本无法找到一种实现它的好方法。所以我有一组我反序列化的 json 文件,所有这些文件都具有相同的结构。

{
urlSouce": """,
  "info": [
    {
      "id": ""
    }
]
}

所以每次我反序列化一个文件时,我都会在一个变量中使用 urlSOurce 并将该值用于 API 请求。但是我知道大多数时候这个 urlSource 是相同的,因此不需要继续发送 API 请求并影响性能,这就是为什么我首先要检查来自当前反序列化文件的 urlSource 是否与上一个文件。

如果是,则无需再次发送 API 请求。我知道我可以在这个逻辑中添加一个布尔标志,但我想不出最好的保存位置。

为了演示我在做什么,我有以下代码。如果有人对需要做什么有任何想法,请帮助我

string previousUrl;
string currentURL;
bool isValueChanged;

currentURL = "test1.com"; //the new URL populated from the deserialized JSON
previousUrl = "test2.com"; //the previously deserialized URL of the file before this

这里我对任何反序列化都没有问题

我遇到的问题是如何跟踪以前的URl 值?以及如何操作 booleanFlag?

【问题讨论】:

  • 您是如何接收数据的?你在下一次之后进行一次反序列化吗?如果是一个接一个,你能不能不简单的比较两个字符串,如果不匹配,做点什么?
  • 据我所知,您有多个字符串进入,您想确定您以前是否遇到过一个。对吗?
  • 是的@RufusL 完全正确

标签: c# .net string performance logic


【解决方案1】:

听起来你想在处理之前验证一个字符串之前没有被使用过。

如果是这种情况,您可以简单地将“使用”字符串存储在Dictionary 中,字符串为Key,处理结果为Value,然后只需检查字典是否已经在处理任何新字符串之前包含一个Key

下面的方法接受一个string 来处理和一个bool 指定是否使用缓存的结果(如果存在)。如果字典中不包含string,或者用户指定了useCache = false,则处理字符串并保存在字典中(连同结果),并返回结果。否则会立即返回该字符串的缓存结果。

private readonly Dictionary<string, string> cachedResults = new Dictionary<string, string>();

public string GetApiResult(string uri, bool useCachedResult = true)
{
    // If we've already encountered this string, return the cached result right away
    if (useCachedResult && cachedResults.ContainsKey(uri)) return cachedResults[uri];

    // Process the string here
    var result = MakeAPICall(uri);

    // Save it, along with the result, in our dictionary
    cachedResults[uri] = result;

    // Return the result
    return result;
}

听起来我误解了这个问题。如果您只是想确定一个新字符串是否与遇到的最后一个字符串匹配,那么这也很简单:

private string cachedUri;
private string cachedResult;

public string GetApiResult(string uri, bool useCachedResult = true)
{
    // If we've already encountered this string, return the cached result right away
    if (useCachedResult && uri == cachedUri) return cachedResult;

    // Process the string here
    var result = MakeAPICall(uri);

    // Save it, along with the result, in our fields
    cachedUri = uri;
    cachedResult = result;

    // Return the result
    return result;
}

【讨论】:

  • 这里有个小问题。所以说我第一次得到 test1.com 作为 Url,第二次得到 test1.com,但第三次得到 newUrl test2.com,第四次得到 test1.com。在这种情况下,我需要第一次发出 api 请求,因为第二次它的相同 url 我不应该发送 api 请求。但是第三次​​它又改变了,所以我必须提出一个新的 API 请求。但在第四次,即使它是我们之前遇到的一个 url,它不是我们拥有的最新 url ...所以我想再次发出 API 请求
  • 我一定误会了——我问“你想确定你以前是否遇到过”,而你说“完全正确”。但是听起来你只想知道新字符串是否与遇到的最后一个字符串匹配?
  • 我添加了另一个样本,该样本仅与上次遇到的 Uri 进行比较。
【解决方案2】:

我会使用 MemoryCache(或只是 Dictionary),将 Url 作为键并将响应的结果作为值,但如果通常只有一个 url 变量就足够了:

string previousUrl = null;
string previousValue = null;
string currentURL;
bool isValueChanged;

while (…)
{
   string result = null;
   currentURL = "test1.com"; //the new URL populated from the deserialized JSON
   if (previousUrl != currentUrl)
   {
      previousResult = GetTheData(currentUrl);
      previousUrl = currentUrl;
   }
   result = previousResult;

   // process result for currentUrl 
}

【讨论】:

    猜你喜欢
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    相关资源
    最近更新 更多