sieve的objective-c实现
用obj-cl来实现前面的sieve代码貌似“丑”了不少,应该有更好的方式:比如不用Foundation或不用NSArray类,而改用其它更“底层”的类。
先把代码贴出来:
//
// main.m
// sieve
//
// Created by kinds on 15/5/2.
// Copyright (c) 2015年 hopy. All rights reserved.
//
#import <Foundation/Foundation.h>
//#include <time.h>
//#include <unistd.h>
typedef unsigned long long ULL;
void zero_array(ULL count,NSMutableArray *ary){
for (ULL i = 0; i < count; i++) {
[ary addObject:[NSNumber numberWithInt:0]];
}
}
ULL sieve_objc(ULL n){
NSMutableArray *ary = [NSMutableArray arrayWithCapacity:n+1];
zero_array(n+1, ary);
//NSLog(@"ary is %lu",[ary count]);
ULL max = sqrtl(n);
ULL p = 2;
NSNumber *x = nil;
while (p<=max) {
for(ULL i=2*p;i<=n;i+=p)
//[ary replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]];
[ary setObject:[NSNumber numberWithInt:1] atIndexedSubscript:i];
x = [ary objectAtIndex:++p];
while ([x intValue]) {
x = [ary objectAtIndex:++p];
}
}
x = [ary objectAtIndex:n];
while ([x intValue]) {
n--;
x = [ary objectAtIndex:n];
}
return n;
}
ULL sieve(ULL n){
char *ary = malloc(n+1);
if(!ary) return 0;
memset(ary,0,n+1);
ULL max = sqrtl(n);
ULL p = 2;
while (p <= max) {
for(ULL i = 2*p;i<=n;i+=p)
ary[i] = 1;
while (ary[++p]); //empty
}
while(ary[n]) n--;
return n;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSProcessInfo *psi = [NSProcessInfo processInfo];
NSArray *args = [psi arguments];
if([args count] != 2){
printf("usage : %s n\n",[[[args objectAtIndex:0] lastPathComponent] UTF8String]);
return 1;
}
long long n = [[args objectAtIndex:1] integerValue];
if (!n) {
puts("you must input a number");
return 2;
}
if(n<0){
puts("you must input a +number");
return 3;
}
clock_t start = clock();
//ULL result = sieve((ULL)n);
ULL result = sieve_objc((ULL)n);
if(!result){
puts("sieve calc failed!");
return 4;
}
double end = ((1.0 * (clock() - start)) / CLOCKS_PER_SEC) * 1000.0;
printf("max p is %llu (take %f ms)\n",result,end);
}
return 0;
}
没找到NSArray中的类或实例方法有可以完成如下简单的数组功能:
//定义10000个指定对象的数组
typedef struct _st_foo{
char name[256];
char note[1024];
int age;
unsigned long long id;
}st_foo,*pst_foo;
st_foo ary[1024]; //NSArray can't do this
所以写了一个zero_array函数来完成该功能,该函数超慢的。
所以可想而知这个obj-c版的效率能有多差,等有机会再优化一下吧。
sieve的objective-c实现的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- 埃拉托色尼筛法(Sieve of Eratosthenes)求素数。
埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的方法.从建立一个整数2~N的表着手,寻找i? 的整数,编程实现此算法,并讨论运算时间. 由于是通过删除来实现, ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- Objective C ARC 使用及原理
手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...
- Objective -C学习笔记之字典
//字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
随机推荐
- Android开源框架ViewPagerIndicator的基本使用
转载本博客请注明出处:点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52495647 很多新闻资讯类的app都有一些共性,那就是 ...
- (NO.00005)iOS实现炸弹人游戏(十一):怪物之火精灵
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 从本篇开始我们一次介绍一下游戏中敌人的制作过程.看过第一篇的小 ...
- Android ListPopupWindow的使用
其实像ListPopupWindow.PopupMenu的用法大致和PopupWindow的一样!就不讲了,相信用过PopupWindow的看一下就能明白. 先上个效果图: ListPopupWind ...
- Mybatis代码自动生成插件使用
1.配置pom.xml 添加mybatis-generator-maven-plugin到pom.xml. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- UNIX网络编程——非阻塞connect
当在一个非阻塞的TCP套接字上调用connect时,connect将立即返回一个EINPROGRESS错误,不过已经发起的TCP三次握手继续进行.我们接着使用select检测这个连接或成功或失败的已建 ...
- Android开发学习之路--UI之自定义布局和控件
新的一年已经开始了,今天已经是初二了,两天没有学习了,还是要来继续学习下.一般手机的title都是actionbar,就像iphone一样可以后退,可以编辑.这里自定义布局就来实现下这个功能,首先准备 ...
- 交叉验证(CrossValidation)方法
分类器模型通常在特定的数据上进行训练,由于所得模型可能存在过拟合的现象.因此,模型训练完成之后通常需要进行检验,以验证分类模型在未知数据集上的预测能力,即我们通常所说的"模型泛化" ...
- Windows7 x64 跨平台开发环境安装配置
======================================================================= Windows7 x64 跨平台开发环境安装配置 201 ...
- 多态原理探究-从C++编译器角度理解多态的实现原理
理论知识: 当类中声明虚函数时,编译器会在类中生成一个虚函数表. 虚函数表是一个存储类成员函数指针的数据结构. 虚函数表是由编译器自动生成与维护的. virtual成员函数会被编译器放入虚函数表中. ...
- Linux IPC实践(4) --System V消息队列(1)
消息队列概述 消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法(仅局限于本机); 每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值. 消息队列也有管道一样的不足: ...