在Mathematica中缓存数据 [英] Caching of data in Mathematica

查看:103
本文介绍了在Mathematica中缓存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个非常耗时的操作,它会在我的程序包中生成一个数据集。我想保存此数据集,仅在手动删除缓存的文件时才让软件包重建它。这是我作为软件包一部分的方法:

there is a very time-consuming operation which generates a dataset in my package. I would like to save this dataset and let the package rebuild it only when I manually delete the cached file. Here is my approach as part of the package:

myDataset = Module[{fname, data}, 
    fname = "cached-data.mx";
    If[FileExistsQ[fname], 
        Get[fname],
        data = Evaluate[timeConsumingOperation[]];
        Put[data, fname];
        data]
];

timeConsumingOperation[]:=Module[{},
    (* lot of work here *)
    {"data"}
];

但是,Put命令没有将长数据集写入文件,而是只写了一行:即使我如上所述用Evaluate包装 timeConsumingOperation []。 (确实,这种行为是不一致的,有时是写入数据集,有时不是。)

However, instead of writing the long data set to the file, the Put command only writes one line: "timeConsumingOperation[]", even if I wrap it with Evaluate as above. (To be true, this behaviour is not consistent, sometimes the dataset is written, sometimes not.)

如何缓存数据?

推荐答案

过去,每当我遇到评估问题时,通常是因为我没有正确匹配函数所需的模式。例如,

In the past, whenever I've had trouble with things evaluating it is usually when I have not correctly matched the pattern required by the function. For instance,

f[x_Integers]:= x

不会匹配任何内容。相反,我的意思是

which won't match anything. Instead, I meant

f[x_Integer]:=x

但是,在您的情况下,没有匹配的模式: timeConsumingOperation []

In your case, though, you have no pattern to match: timeConsumingOperation[].

您的问题更可能与相对于 myDataset定义 timeConsumingOperation 的时间有关。在上面发布的代码中, timeConsumingOperation 是在 myDataset 之后定义的。因此,在第一次运行时(或在清除全局变量后立即),您将得到准确的描述结果,因为在代码中未定义 timeConsumingOperation 时, myDataset 运行。

You're problem is more likely related to when timeConsumingOperation is defined relative to myDataset. In the code you've posted above, timeConsumingOperation is defined after myDataset. So, on the first run (or immediately after you've cleared the global variables) you would get exactly the result you're describing because timeConsumingOperation is not defined when the code for myDataset is run.

现在, SetDelayed := )会自动使该变量在使用时重新计算,并且由于不需要传递任何参数,因此不需要方括号。这里的重点是, timeConsumingOperation 可以在 myDataset 之前声明为书面形式,因为 SetDelayed 将导致它在使用之前不被执行。

Now, SetDelayed (:=) automatically causes the variable to be recalculated whenever it is used, and since you do not require any parameters to be passed, the square brackets are not necessary. The important point here is that timeConsumingOperation can be declared, as written, prior to myDataset because SetDelayed will cause it not to be executed until it is used.

总而言之,您的缓存方法看起来完全可以满足我的需求。

All told, your caching methodology looks exactly how I would go about it.

这篇关于在Mathematica中缓存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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