在运行之前在 iPhone 上创建并存储前缀树
我目前正在为 iOS 制作一款文字游戏,加载时会读取大约 30000 个单词的文本文件,并将它们加载到前缀树中,以便在游戏过程中快速搜索。这效果很好,但加载和树构建过程明显增加了应用程序的启动时间几秒钟。目前我正在 iPhone 4 上进行测试,但我想在 3GS 早期型号上速度会慢很多。
有没有办法在编译时而不是在应用程序打开时创建此树?或者,不太理想的是,是否可以使用另一个程序预烘焙数据并将该文件添加到项目中,而不是在运行时执行此操作?我该怎么做呢?
I'm currently making a word game for iOS that, when loading, reads in a text file of around 30000 words and loads them into a prefix tree for quick searching during gameplay. This works well, but the loading and tree construction process adds a noticeable few seconds to the app's startup time. At the moment I'm testing on an iPhone 4, but I imagine it would be a good deal slower on a 3GS earlier models.
Is there a way to create this tree at compile time rather than when the app opens? Or, less ideally, would it be possible to prebake the data with another program and add that file to the project instead of doing it at runtime? How would I go about doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在我们开发的游戏中遇到了同样的问题,对于我们来说,使用带有单词的 SQLite DB 而不是内存中的树表现得更好。 DB 使用的空间比表示树的 plist 少,它不需要我们将其预加载到内存中,并且性能(查询有效单词时)大致相同。
I run into the same issue with a game we developed, and for us, it performed better to use a SQLite DB with the words instead of the in-memory tree. The DB used less space than the plist that represented the tree, it did not require us to preload it in memory, and the performance (when querying for a valid word) was about the same.
pgb的回答很好。如果您不想使用 SQLite,您可以将数据存储在 plist 中,并让 [NSDictionary DictionaryWithContentsOfFile:] 为您创建一棵树。
如果您确实选择将数据编译到程序中,则必须由基本类型(例如数字和字符)构建。然后,使用结构体和数组来定义结构体,并使用常量变量来存储数据。这是一个简单的例子,它只是一个字符串数组:
pgb's answer is good. If you do not want to use SQLite, you can store your data in a plist and have [NSDictionary dictionaryWithContentsOfFile:] create a tree for you.
If you do choose to have the data compiled into your program, it will have to be built of primitive types, such as numbers and characters. Then, use structures and arrays to define the structure, and use a constant variable to store the data. Here is a simple example which is just an array of character strings: