将随机数转换为 XY 坐标以进行绘图
我的教授给我们布置了一项作业,要求我们使用线性和搜索来测试运行时间和搜索大小的差异。二进制算法,数据将被绘制成图表。
我有搜索方法将运行时 &数组大小作为 ArrayList 中的点,然后发送到 GraphResults 类进行绘图。我需要先将这些数据点转换为 xy 坐标。搜索大小是 x 轴,运行时间是 y 轴
由于搜索大小固定为 128 的倍数,并且只有 8 个大小,我使用 switch 来计算 x 值,但我正在寻找一种更有效的方法将运行时间转换为坐标。
现在,我正在使用 5 个嵌套条件,如下所示:
if (y<=1000) {
if (y<= 500) {
if (y<= 250) {
newy= yaxis-32; }//equals to 250ms category
else {
newy= yaxis-(32*2); }//500ms category
}
else if (y<=750) {
newy= yaxis-(32*3);} //750ms category
else {
newy= yaxis-(32*4);} //1000ms category
} //end of the 1000ms tests
现在,超过 5000 毫秒的数字需要 7 次测试。有没有更有效的方法根据数字大小分配数字?
My professor gave us an assignment to test the difference in runtimes and search sizes using linear & binary algorithms, and the data is to be graphed.
I have the search methods put the runtime & array sizes as Points in an ArrayList, which is then sent to the GraphResults class for plotting. I need to convert those data points into xy coordinates before. The search size is the x-axis and the runtime is the y axis
As the search sizes are fixed as a multiple of 128 and there are only 8 sizes, I used switch for calculating the x value, but am looking for a more efficient way to convert the runtimes into coordinates.
Right now, I'm using nested conditionals with 5 like this:
if (y<=1000) {
if (y<= 500) {
if (y<= 250) {
newy= yaxis-32; }//equals to 250ms category
else {
newy= yaxis-(32*2); }//500ms category
}
else if (y<=750) {
newy= yaxis-(32*3);} //750ms category
else {
newy= yaxis-(32*4);} //1000ms category
} //end of the 1000ms tests
Right now, the numbers that are over 5000ms require 7 tests. Is there a more efficient way to assign a number based on a number size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您尝试确定测量范围时,可以将金额除以范围大小,然后计算要在图表中显示的数字。
顺便说一句,在您的代码中,您犯了一个逻辑错误,如果值为
y <= 1000
,则第一个条件计算为 true,第二个条件为y <= 750
> 永远不会被评估。而且,值范围越高,图表点就越低。是这样的吗? (1000 -> ymax - 128 while 1 -> ymax - 32)
另外,如果您想将值与不均匀的范围进行比较,您还可以执行类似数组查找的操作(伪代码):
请注意,输出 -范围索引充当大于数组中最大值的值的范围。
As you are trying to determine the range of your measurement, you can divide the amount by the range size, followed by calculating the number you want to show in the graph.
Btw, in your code, you made a logic error, if the value is
y <= 1000
the first condition evaluates to true, and the second fory <= 750
will never be evaluated.Also it seems that the higher the value range, the lower your graph point. Is that as intended? (1000 -> ymax - 128 while 1 -> ymax - 32)
As an aside, if you want to compare values to uneven ranges, you can also do something like an array lookup (pseudo code):
Note that the out-of-range index acts as the range found for a value that is bigger than the biggest value in your array.
newy = yaxis - 32 * ((y/250)% 8); 怎么样?
How about
newy = yaxis - 32 * ((y/250)% 8);?
我会将您的代码重新格式化为更像这样的内容:
这样,您将计算乘数而不是手动选择它。
I would reformat your code to something more like this:
This way, you're calculating the multiplier rather than choosing it manually.