CAS非阻塞锁和synchronized重量级锁比较.docx_第1页
CAS非阻塞锁和synchronized重量级锁比较.docx_第2页
CAS非阻塞锁和synchronized重量级锁比较.docx_第3页
CAS非阻塞锁和synchronized重量级锁比较.docx_第4页
CAS非阻塞锁和synchronized重量级锁比较.docx_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

cassandra分页查询案例,CAS非阻塞锁和synchronized重量级锁比较,Chromeapp实现网易云音乐歌单播放器,实现网易云音乐歌单播放器,C-minus语言编译器的词法分析部分Crc校验,通过算法验证数据的完整性,css3实现渐变色文字和镂空文字CSS,cwRsync_4.0.5windows2008下配置,C调用Go的回调函数传递字符串代码 函数返回多个对象 # Goals: To write functions# To write functions that send back multiple objects.# FIRST LEARN ABOUT LISTS -X = list(height=5.4, weight=54)print(Use default printing -)print(X)print(Accessing individual elements -)cat(Your height is , X$height, and your weight is , X$weight, n)/C语言实现双向链表/C语言实现统计字符个数# FUNCTIONS -square - function(x) return(x*x)cat(The square of 3 is , square(3), n) # default value of the arg is set to 5.cube - function(x=5) return(x*x*x);cat(Calling cube with 2 : , cube(2), n) # will give 23cat(Calling cube : , cube(), n) # will default to 53.# LEARN ABOUT FUNCTIONS THAT RETURN MULTIPLE OBJECTS -powers - function(x) parcel = list(x2=x*x, x3=x*x*x, x4=x*x*x*x); return(parcel);/C语言随机函数rand实现/C语言顺序表完整实例/C语言实现文件的des加解密实例X = powers(3);print(Showing powers of 3 -); print(X);# WRITING THIS COMPACTLY (4 lines instead of 7)powerful - function(x) return(list(x2=x*x, x3=x*x*x, x4=x*x*x*x);print(Showing powers of 3 -); print(powerful(3);# In R, the last expression in a function is, by default, what is# returned. So you could equally just say:powerful - function(x) list(x2=x*x, x3=x*x*x, x4=x*x*x*x) 代码 向量符号 # Goal: The amazing R vector notation.cat(EXAMPLE 1: sin(x) for a vector -n)# Suppose you have a vector x -x = c(0.1,0.6,1.0,1.5)# The bad way -n = length(x)r = numeric(n)for (i in 1:n) ri = sin(xi)print(r)# The good way - dont use loops -print(sin(x)cat(nnEXAMPLE 2: Compute the mean of every row of a matrix -n)# Heres another example. It isnt really about R; its about thinking in# matrix notation. But still.# Let me setup a matrix -N=4; M=100;r = matrix(runif(N*M), N, M)# So I face a NxM matrix# r11 r12 . r1N# r21 r22 . r2N# r32 r32 . r3N# My goal: each column needs to be reduced to a mean.# Method 1 uses loops:mean1 = numeric(M)for (i in 1:M) mean1i = mean(r,i)# Alternatively, just say:mean2 = rep(1/N, N) %*% r # Pretty!# The two answers are the same -all.equal(mean1,mean2,)# As an aside, I should say that you can do this directly by using# the rowMeans() function. But the above is more about pedagogy rather# than showing you how to get rowmeans.cat(nnEXAMPLE 3: Nelson-Siegel yield curven)# Write this asif youre dealing with scalars -# Nelson Siegel functionnsz - function(b0, b1, b2, tau, t) tmp = t/tau tmp2 = exp(-tmp) return(b0 + (b1+b2)*(1-tmp2)/(tmp) - (b2*tmp2)timepoints - c(0.01,1:5)# The bad way:z - numeric(length(timepoints)for (i in 1:length(timepoints) zi - nsz(14.084,-3.4107,0.0015,1.8832,timepointsi)print(z)# The R way -print(z - nsz(14.084,-3.4107,0.0015,1.8832,timepoints)cat(nnEXAMPLE 3: Making the NPV of a bond-n)# You know the bad way - sum over all cashflows, NPVing each.# Now look at the R way.C = rep(100, 6)nsz(14.084,-3.4107,0.0015,1.8832,timepoints) # Print interest ratesC/(1.05)timepoints) # Print cashflows discounted 5%C/(1 + (0.01*nsz(14.084,-3.4107,0.0015,1.8832,timepoints)timepoints) # Using NS instead of 5%# NPV in two different ways -C %*% (1 + (0.01*nsz(14.084,-3.4107,0.0015,1.8832,timepoints)-timepointssum(C * (1 + (0.01*nsz(14.084,-3.4107,0.0015,1.8832,timepoints)-timepoints)# You can drop back to a flat yield curve at 5% easily -sum(C * 1.05-timepoints)# Make a function for NPV -npv - function(C, timepoints, r) return(sum(C * (1 + (0.01*r)-timepoints)npv(C, timepoints, 5)# Bottom line: Heres how you make the NPV of a bond with cashflows C# at timepoints timepoints when the zero curve is a Nelson-Siegel curve -npv(C, timepoints, nsz(14.084,-3.4107,0.0015,1.8832,timepoints)# Wow!# -# Elegant vector notation is amazingly fast (in addition to being beautiful)N - 1e5x - runif(N, -3,3)y - runif(N)method1 - function(x,y) tmp - NULL for (i in 1:N) if (xi 0) tmp - c(tmp, yi) tmpmethod2 - function(x,y) yx 0s1 - system.time(ans1 - method1(x,y)s2 - system.time(ans2 - method2(x,y)all.equal(ans1,ans2)s1/s2 # On my machine its 2000x faster 代码 索引符号 # Goal: To show amazing R indexing notation, and the use of is.na()x - c(2,7,9,2,NA,5) # An example vector to play with.# Give me elems 1 to 3 -x1:3# Give me all but elem 1 -x-1# Odd numbered elements -indexes - seq(1,6,2)xindexes# or, more compactly,xseq(1,6,2)# Access elements by specifying on / off through booleans -require - c(TRUE,TRUE,FALSE,FALSE,FALSE,FALSE)xrequire# Short vectors get reused! So, to get odd numbered elems -xc(TRUE,FALSE)# Locate missing data -is.na(x)# Replace missing data by 0 -xis.na(x) - 0x# Similar ideas work for matrices -y - matrix(c(2,7,9,2,NA,5), nrow=2)y# Make a matrix containing columns 1 and 3 -y,c(1,3)# Let us see what is.na(y) does -is.na(y)str(is.na(y)# So is.na(y) gives back a matrix with the identical structure as that of y.# Hence I can sayyis.na(y) - -1y 代码 创建LATEX表格的矩阵 # Goal: To make latex tabular out of an R matrix# Setup a nice R object:m - matrix(rnorm(8), nrow=2)rownames(m) - c(Age, Weight)colnames(m) - c(Person1, Person2, Person3, Person4)m# Translate it into a latex tabular:library(xtable)xtable(m, digits=rep(3,5)# Production latex code that goes into a paper or a book -print(xtable(m, caption=String, label=t:), type=latex, file=blah.gen, table.placement=tp, latex.environments=c(center, footnotesize)# Now you do inputblah.gen in your latex file.# Youre lazy, and want to use R to generate latex tables for you?data - cbind( c(7,9,11,2), c(2,4,19,21) )colnames(data) - c(a,b)rownames(data) - c(x,y,z,a)xtable(data)# or you could dodata - rbind( c(7,2), c(9,4), c(11,19), c(2,21) )# and the rest goes through identically. 代码 数组,散列表和寻址 # Goal: Associative arrays (as in awk) or hashes (as in perl).# Or, more generally, adventures in R addressing.# Heres a plain R vector:x - c(2,3,7,9)# But now I tag every elem with labels:names(x) - c(kal,sho,sad,aja)# Associative array operations:xkal - 12# Pretty printing the entire associative array:x# This works for matrices too:m - matrix(runif(10), nrow=5)rownames(m) - c(violet,indigo,blue,green,yellow)colnames(m) - c(Asia,Africa)# The full matrix -m# Or even better -library(xtable)xtable(m)# Now address symbolically -m,Africamindigo,mindigo,Africa# The in operator, as in awk -for (colour in c(yellow, orange, red) if (colour %in% rownames(m) cat(For Africa and , colour, we have , mcolour, Africa, n) else cat(Colour , colour, does not exist in the hash.n) # This works for data frames also -D - data.frame(m)D# Look closely at what happened -str(D) # The colours are the rownames(D).# Operations -D$AfricaD,AfricaDyellow,# orsubset(D, rownames(D)=yellow)colnames(D) - c(Antarctica,America)DD$America 代码 矩阵符号 # Goal: Utilise matrix notation# We use the problems of portfolio analysis as an example.# Prices of 4 firms to play with, at weekly frequency (for calendar 2004) -p - structure(c(300.403, 294.604, 291.038, 283.805, 270.773, 275.506, 292.271, 292.837, 284.872, 295.037, 280.939, 259.574, 250.608, 268.84, 266.507, 263.94, 273.173, 238.609, 230.677, 192.847, 219.078, 201.846, 210.279, 193.281, 186.748, 197.314, 202.813, 204.08, 226.044, 242.442, 261.274, 269.173, 256.05, 259.75, 243, 250.3, 263.45, 279.5, 289.55, 291.95, 302.1, 284.4, 283.5, 287.8, 298.3, 307.6, 307.65, 311.9, 327.7, 318.1, 333.6, 358.9, 385.1, 53.6, 51.95, 47.65, 44.8, 44.85, 44.3, 47.1, 44.2, 41.8, 41.9, 41, 35.3, 33.35, 35.6, 34.55, 35.55, 40.05, 35, 34.85, 28.95, 31, 29.25, 29.05, 28.95, 24.95, 26.15, 28.35, 29.4, 32.55, 37.2, 39.85, 40.8, 38.2, 40.35, 37.55, 39.4, 39.8, 43.25, 44.75, 47.25, 49.6, 47.6, 46.35, 49.4, 49.5, 50.05, 50.5, 51.85, 56.35, 54.15, 58, 60.7, 62.7, 293.687, 292.746, 283.222, 286.63, 259.774, 259.257, 270.898, 250.625, 242.401, 248.1, 244.942, 239.384, 237.926, 224.886, 243.959, 270.998, 265.557, 257.508, 258.266, 257.574, 251.917, 250.583, 250.783, 246.6, 252.475, 266.625, 263.85, 249.925, 262.9, 264.975, 273.425, 275.575, 267.2, 282.25, 284.25, 290.75, 295.625, 296.25, 291.375, 302.225, 318.95, 324.825, 320.55, 328.75, 344.05, 345.925, 356.5, 368.275, 374.825, 373.525, 378.325, 378.6, 374.4, 1416.7, 1455.15, 1380.97, 1365.31, 1303.2, 1389.64, 1344.05, 1266.29, 1265.61, 1312.17, 1259.25, 1297.3, 1327.38, 1250, 1328.03, 1347.46, 1326.79, 1286.54, 1304.84, 1272.44, 1227.53, 1264.44, 1304.34, 1277.65, 1316.12, 1370.97, 1423.35, 1382.5, 1477.75, 1455.15, 1553.5, 1526.8, 1479.85, 1546.8, 1565.3, 1606.6, 1654.05, 1689.7, 1613.95, 1703.25, 1708.05, 1786.75, 1779.75, 1906.35, 1976.6, 2027.2, 2057.85, 2029.6, 2051.35, 2033.4, 2089.1, 2065.2, 2091.7), .Dim = c(53, 4), .Dimnames = list(NULL, c(TISCO, SAIL, Wipro, Infosys)# Shift from prices to returns -r - 100*diff(log(p)# Historical expected returns -colMeans(r)# Historical correlation matrix -cor(r)# Historical covariance matrix -S - cov(r)S# Historical portfolio variance for a stated portfolio of 20%,20%,30%,30% -w - c(.2, .2, .3, .3)t(w) %*% S %*% w# The portfolio optimisation function in tseries -library(tseries)optimised - portfolio.optim(r) # This uses the historical facts from roptimised$pw # Weightsoptimised$pm # Expected return using these weightsoptimised$ps # Standard deviation of optimised port. 代码 处理丢失数据 # Goal:# A stock is traded on 2 exchanges.# Price data is missing at random on both exchanges owing to non-trading.# We want to make a single price time-series utilising information# from both exchanges. I.e., missing data for exchange 1 will# be replaced by information for exchange 2 (if observed).# Lets create some example data for the problem.e1 - runif(15) # Prices on exchange 1e2 - e1 + 0.05*rnorm(15) # Prices on exchange 2.cbind(e1, e2)# Blow away 5 points from each at random.e1sample(1:15, 5) - NAe2sample(1:15, 5) - NAcbind(e1, e2)# Now how do we reconstruct a time-series that tries to utilise both?combined - e1 # Do use the more liquid exchange here.missing - is.na(combined)combinedmissing - e2missing # if its also missing, I dont care.cbind(e1, e2, combined)# There you are. 代码 读取数据文件查看内容 # Goal: To read in a simple data file, and look around its contents.# Suppose you have a file x.data which looks like this:# 1997,3.1,4# 1998,7.2,19# 1999,1.7,2# 2000,1.1,13# To read it in -A - read.table(x.data, sep=, s=c(year, my1, my2)nrow(A) # Count the rows in Asummary(A$year) # The column year in data frame A # is accessed as A$yearA$newcol - A$my1 + A$my2 # Makes a new column in Anewvar - A$my1 - A$my2 # Makes a new R object newvarA$my1 - NULL # Removes the column my1# You might find these useful, to look around a dataset -str(A)summary(A)library(Hmisc) # This requires that youve installed the Hmisc packagecontents(A)describe(A) 代码 读取带日期数据文件 # Goal: To read in a simple data file where date data is present.# Suppose you have a file x.data which looks like this:# 1997-07-04,3.1,4# 1997-07-05,7.2,19# 1997-07-07,1.7,2# 1997-07-08,1.1,13A - read.table(x.data, sep=, s=c(date, my1, my2)A$date - as.Date(A$date, format=%Y-%m-%d) # Say ?strptime to learn how to use % to specify # other date formats. Two examples - # 15/12/2002 needs %d/%m/%Y # 03 Jun 1997 needs %d %b %Y # Actually, if youre using the ISO 8601 date format, i.e. # %Y-%m-%d, thats the default setting and you dont need to # specify the format.A$newcol - A$my1 + A$my2 # Makes a new column in Anewvar - A$my1 - A$my2 # Makes a new R object newvarA$my1 Bool - Boolprop_isSubstringOf as bs = (as isSubstringOf bs) = (as isSubstringOf2 bs) Test.QuickCheck.test prop_isSubstringOfOK, passed 100 tests.代码 时间日期处理 Erlang offers calendar module which provides computation of date, time and number of date time conversion functions. For more details, please refer to documentation for this module Here are some examples: Data types Date = Year, Month, Day Time = Hour, Minute, Second where Year = an integer and cannot be abbreviated. E.g.: 93 denotes year 93, not 1993 Month = 1.12 Day = 1.31 Hour = 0.23 Minute = 0.59 Second = 0.59 How to obtain local date time Date, Time = calendar:local_time()1 Current = calendar:local_time().2009,9,7,12,32,222 Current_Year, Current_Month, Current_Day, _ = Current.2009,9,7,12,32,223 Current_Year.20094 Current_Month.95 Current_Day.76 _, Current_Hour, Current_Min, Current_Second = Current.2009,9,7,12,32,227 Current_Hour.128 Current_Min.329 Current_Second.22 How to obtain current UTC time Date, Time = calendar:universal_time() How to convert time to seconds since midnight and via versa Seconds = calendar: time_to_seconds(Time) Time = calendar:seconds_to_time(Seconds)10 Time1 = 10, 6, 30.10,6,3011 Seconds = calendar:time_to_seconds(Time1).3639012 calendar:seconds_to_time(Seconds).10,6,30 How to verify if a year is leap year Bool = calendar:is_leap_year(Year)14 calendar:is_leap_year(2008).TRUE15 calendar:is_leap_year(2009).FALSE How to check if a date is valid Bool = calendar:valid_date(Date) Bool = calendar:valid_date(Year, Month, Day)16 calendar:valid_date(2009,12,12).TRUE17 calendar:valid_date(2009,13,12).FALSE18 calendar:valid_date(2009, 12, 31).TRUE19 calendar:valid_date(2008, 2, 29).TRUE How to find out day of the week DayNumber = calendar:day_of_the_week(Date) DayNumber = calendar:day_of_the_week(Year, Month, Day) 1 = Monday, 2 = Tuesday, .and 7 = Sunday20 calendar:day_of_the_week(2009,9,7).121 calendar:day_of_the_week(2009, 9, 9).3 How to find out last day of a month LastDay = calendar:last_day_of_the_month(Year, Month)22 calendar:last_day_of_the_month(2008, 2).2923 calendar:last_day_of_the_month(2009, 8).31 How to calculate date time difference Days, Time = calendar:time_difference(DT1, DT2) DT1 = Date1, Time1 DT2 = Date2, Time2 Logically equivalent to DT2 DT17 Date1=2009,8,30, 23,0,0.2009,8,30,23,0,08 Date2=2009,9,30, 23,0,0.2009,9,30,23,0,09 calendar:time_difference(Date2, Date1).-31,0,0,010 calendar:time_difference(Date1, Date2).31,0,0,011 Date3=2008,8,30,23,0,0.2008,8,30,23,0,012 calendar:time_difference(Date1, Date3).-365,0,0,0代码 生成和检查质数 Many applications require the use of large prime numbers. The module primes.erl can be used to generate large primes and for primality testing. primes.erl exports the following: make(N) - IGenerates a random integer I with N decimal digits. is_prime(N) - bool()Tests if N is a prime number make_prime(K) - PGenerates a random prime P with at least K decimal digits. make(N) is easy: make(N) - new_seed(), make(N, 0). make(0, D) - D; make(N, D) - make(N-1, D*10 + (random:uniform(10)-1).where: new_seed() - _,_,X = erlang:now(), H,M,S = time(), H1 = H * X rem 32767, M1 = M * X rem 32767, S1 = S * X rem 32767, put(random_seed, H1,M1,S1).is_prime(N) is more interesting. We use a probabilistic m

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论